0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ASP.NET]Ajaxを利用して動画ファイルのアップロード・サムネイルを作成を行う。

Last updated at Posted at 2019-05-31
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);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?