Webapi formdata upload (to DB) with extra parameters

后端 未结 4 1718
谎友^
谎友^ 2020-11-28 04:47

I need to upload file sending extra paramaters.

I have found the following post in stackoverflow: Webapi ajax formdata upload with extra parameters

It descri

4条回答
  •  迷失自我
    2020-11-28 05:17

    Ultimately, the following was what worked for me:

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    
    var provider = new MultipartFormDataStreamProvider(root);
    
    var filesReadToProvider = await Request.Content.ReadAsMultipartAsync(provider);
    
    foreach (var file in provider.FileData)
    {
        var fileName = file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
        byte[] documentData;
    
        documentData = File.ReadAllBytes(file.LocalFileName);
    
        DAL.Document newRecord = new DAL.Document
        {
            PathologyRequestId = PathologyRequestId,
            FileName = fileName,
            DocumentData = documentData,
            CreatedById = ApplicationSecurityDirector.CurrentUserGuid,
            CreatedDate = DateTime.Now,
            UpdatedById = ApplicationSecurityDirector.CurrentUserGuid,
            UpdatedDate = DateTime.Now
        };
    
        context.Documents.Add(newRecord);
    
        context.SaveChanges();
    }
    

提交回复
热议问题