以下对我有用:
[HttpPost]
public ActionResult Upload(int? chunk, string name)
{
var fileUpload = Request.Files[0];
var uploadPath = Server.MapPath("~/App_Data");
chunk = chunk ?? 0;
using (var fs = new FileStream(Path.Combine(uploadPath, name), chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
return Json(new { message = "chunk uploaded", name = name });
}
在客户端:
$('#uploader').pluploadQueue({
runtimes: 'html5,flash',
url: '@Url.Action("Upload")',
max_file_size: '5mb',
chunk_size: '1mb',
unique_names: true,
multiple_queues: false,
preinit: function (uploader) {
uploader.bind('FileUploaded', function (up, file, data) {
// here file will contain interesting properties like
// id, loaded, name, percent, size, status, target_name, ...
// data.response will contain the server response
});
}
});
就奖金问题而言,我愿意通过不使用会话来回答它,因为它们不能很好地扩展,但是因为我知道你可能不喜欢这个答案,所以你有可能通过会话请求中的 id 使用multipart_params
:
multipart_params: {
ASPSESSID: '@Session.SessionID'
},
然后在服务器上执行一些 hack以创建正确的会话。