Ng-File-Upload to Save PDF Blob to Database

痴心易碎 提交于 2019-12-06 09:52:35

You need to extract the file content out of the form data.

Below is how I do this (using ng-file-upload in the same manner as you from the front end) to upload attachments in my application.

public async Task<IHttpActionResult> UploadAttachment()
{
    // Check if the request contains multipart/form-data.

    try
    {
        var provider = new MultipartMemoryStreamProvider();
        await Request.Content.ReadAsMultipartAsync(provider);

        var f = provider.Contents.First(); // assumes that the file is the only data

        if (f != null)
        {
            var filename = f.Headers.ContentDisposition.FileName.Trim('\"');
            filename = Path.GetFileName(filename);
            var buffer = await f.ReadAsByteArrayAsync();

            //buffer now contains the file content,
            //and filename has the original filename that was uploaded

            //do some processing with it (e.g. save to database)
        }
        else
        {
            return BadRequest("Attachment failed to upload");
        }
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }

    return Ok();
}
Andrés Nava - .NET

When you configure the upload, you specify the URL where the file will be posted to:

file.upload = Upload.upload({
    url: 'myMVC/MyMethod',
    data: {file: file}
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!