起因
最近遇到一个上传视频大文件的需求,文件最少是1个G的视频文件.一直以来没做过这么大的上传文件的功能.原先都是限制上传大小.
在有HTML5了以后,可以对文件进行分割上传.这里是用plupload + Asp.Net Core来实现,其实是新瓶装老酒.
遇到的问题:
1. 怎么给上传文件重命名呢? 是在前台处理呢? 还是后台处理呢?
前台页面代码
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div id="uploadContainer">
<input type="button" value="选择文件" id="btnBrowse" />
<input type="button" value="上传文件" id="btnUpload" />
<ul id="fileList"></ul>
</div>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/js/plupload/plupload.full.min.js"></script>
<script type="text/javascript">
var $uploadContainer = $('#uploadContainer'),
$fileList = $('#fileList'),
$btnUpload = $('#btnUpload');
var uploader = new plupload.Uploader({
browse_button: 'btnBrowse',
runtimes: 'html5,flash,silverlight,html4',
url: '/UploadFile/UploadVideo',
flash_swf_url: 'plupload/Moxie.swf',
sliverlight_xap_url: 'plupload/Moxie.xap',
max_retries: 3,
chunk_size: '10mb',
multi_selection: false,
multipart_params: {
uploadFileName: ''
},
filters: {
mime_types: [
{
title: "视频文件",
extensions: "wmv,avi,mp4",
max_file_size: '4096mb'
}
]
}
});
var urls = [];
var event;
uploader.init();
uploader.bind('FilesAdded', function (uploader, files) {
var value = files[0];
var fileName = value.name,
html = '<li id="file-' + value.id + '">' +
'<p class="file-name">' + fileName + '</p>' +
'<p class="progress"></p>' +
'</li>';
$fileList.html(html);
});
uploader.bind('UploadProgress', function (uploader, file) {
$fileList.find('.progress').text(file.percent + '%');
});
uploader.bind('FileUploaded', function (uploader, file, responseObject) {
var url = responseObject.response;
urls.push(url);
});
uploader.bind('UploadComplete', function (uploader, files) {
var value = urls[0];
var result = JSON.parse(value);
if (result.isSuccess) {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/UploadFile/SaveVideoURL',
data: {
'videoURL': result.videoURL
},
success: function (data) {
if (data.isSuccess) {
alert('上传文件成功!');
}
else {
alert('上传文件失败!');
}
}
});
}
urls = [];
$fileList.html('');
});
$btnUpload.click(function (e) {
event = e;
uploader.setOption('multipart_params', {
'uploadFileName': new Date().getTime()
});
uploader.start();
});
function getFileName() {
return new Date().getTime();
}
</script>
</body>
</html>
后台代码
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
namespace qiufeng.core.Controllers
{
public class UploadFileController : Controller
{
private IHostingEnvironment _hostEnv;
public UploadFileController(IHostingEnvironment env)
{
_hostEnv = env;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult UploadVideo()
{
var files = Request.Form.Files;
if (files != null)
{
string uploadFileName = Request.Form["uploadFileName"];
string fileName = Request.Form["name"];
string extName = Path.GetExtension(fileName);
fileName = $"{uploadFileName}{extName}";
string filePath = Path.Combine(_hostEnv.ContentRootPath, "UploadFile", fileName);
string videoURL = $"/UploadFile/{fileName}";
int chunk = string.IsNullOrEmpty(Request.Form["chunk"]) ? 0 : int.Parse(Request.Form["chunk"]);
foreach (var file in files)
{
using (var stream = new FileStream(filePath, chunk == 0 ? FileMode.CreateNew : FileMode.Append))
{
file.CopyTo(stream);
}
}
return Json(new { isSuccess = true, videoURL = videoURL });
}
return Json(new { isSuccess = false, videoURL = "" });
}
[HttpPost]
public IActionResult SaveVideoURL()
{
bool isSuccess = false;
string videoURL = Request.Form["videoURL"];
if (!string.IsNullOrEmpty(videoURL))
{
isSuccess = true;
}
return Json(new { isSuccess = isSuccess });
}
}
}