sample.aspx
function file_upload(files, index = 0) {
var file = files[index];
var data = new FormData();
data.append(file.name, file);
$.ajax({
url: '<%= VirtualPathUtility.ToAbsolute("~/api/FileUpload") %>',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false,
dataType: 'html',
})
.catch((...args) => {
})
.then(() => {
var has_next = ((files.length - 1) != index);
if (has_next) {
file_upload(files, (index + 1));
} else {
window.location.reload();
}
});
}
$(document).on('click', '#upload_button', function () {
var files = $('#upload_files').get(0).files;
if (files.length > 0) {
file_upload(files);
}
}
FileUploadController.cs
private FFMpegConverter FFMpeg = new FFMpegConverter();
/// <summary>
/// 動画のアップロードとサムネイルの作成
/// </summary>
/// <returns></returns>
public HttpResponseMessage Post()
{
foreach (string name in HttpContext.Current.Request.Files)
{
string pathVideo = $"{VideoFolder}{name}";
string pathThumb = $"{ThumbFolder}{Path.GetFileNameWithoutExtension(name)}.jpg";
// 動画の保存
HttpContext.Current.Request.Files[name].SaveAs(pathVideo);
// サムネイル作成
FFMpeg.GetVideoThumbnail(pathVideo, pathThumb);
}
return HttpContext.Current.Request.Files.Count > 0 ? Request.CreateResponse(HttpStatusCode.Created) : Request.CreateResponse(HttpStatusCode.BadRequest);
}