.NET MVC Web API将文件上传到本地存储并将数据模型上传到数据库

时间:2018-09-18 08:04:03

标签: asp.net-web-api asp.net-web-api2

我想上传一个文件并通过它传递模型。但是当我从邮递员那里尝试时,总是会出错。

这是我的代码

public async Task<IHttpActionResult> PostArPumPd([FromBody] tx_arPumPd pum)
    {          
        try
          {
            if (pum == null)
            {
                return Content(HttpStatusCode.BadRequest, "Enter the data correctly");
            }
            else
            {
            tx_arPumPd arpumpds = new tx_arPumPd()
            {
                doc_no = doc,
                doc_date = DateTime.Now,
                descs = pum.descs,
                currency_code = pum.currency_code,
                amount = pum.amount,
                employee_code = pum.employee_code,
                head_code = pum.head_code,
                company_code = pum.company_code,
                created_by = pum.emp_code,
                created_date = DateTime.Now
            };
            db.tx_arPumPd.Add(arpumpds);

            var multiFormDataStreamProvider = new MultiFileUploadProvider(Constants.MEDIA_PATH);                   

            var mod = "PP";
            var newFileName = mod + "_" + doc;

            await Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);

            try
            {
                await FileHelper.Upload(multiFormDataStreamProvider, mod, newFileName);
                db.SaveChanges();
                return Content(HttpStatusCode.Created, "Data was save");
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.BadRequest, ex);
            }                    
        }
    }
    catch (Exception ex)
    {
        return Content(HttpStatusCode.BadRequest, ex);
    }
}

进入此部分时出现错误 等待Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);

这是错误

  ioexception:mime多部分流的意外结束。哑剧   邮件与邮递员的测试不完整

有人知道为什么吗?并帮助我上传文件和数据模型吗? 非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。为了保存文件和表格数据,我不解析模型。我只是使用表单数据从邮递发送数据,其值为Json数据。因此,我从ReadAsMultipartAsync读取了请求并获取了Json数据,然后反序列化了json。之后,我们可以保存数据。

这就是我从邮递区发送数据的方式 Postmant

这是代码

public async Task<IHttpActionResult> PostArPumPd()
    {          
        try
          {
            var multiFormDataStreamProvider = new MultiFileUploadProvider(Constants.MEDIA_PATH);
            var readToProvider = await Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);

            // Get Json Data and Deserialize it
            var json = await readToProvider.Contents[0].ReadAsStringAsync();
            tx_arPumPd a = JsonConvert.DeserializeObject<tx_arPumPd>(json);

            // Set mod and file name for FileHelper classl
            var mod = "PP";
            var newFileName = mod + "_" + doc ;

            tx_arPumPd arpumpds = new tx_arPumPd()
            {
                doc_no = doc,
                doc_date = DateTime.Now,
                descs = a.descs,
                currency_code = a.currency_code,
                amount = a.amount,
                employee_code = a.employee_code,
                head_code = a.head_code,
                company_code = a.company_code,
                created_by = a.emp_code,
                created_date = DateTime.Now
            };
            db.tx_arPumPd.Add(arpumpds);

            var dtl = a.tx_arPumPdDtl;

            foreach (var item in dtl)
            {
                item.doc_no         = doc;
                item.bg_is_ok       = true;
                item.bg_approved    = true;
                item.bg_app_date    = DateTime.Now;
                item.created_by     = a.emp_Code;
                item.created_date   = DateTime.Now;
                db.tx_arPumPdDtl.Add(item);
            }

            try
            {
                await FileHelper.Upload(multiFormDataStreamProvider, mod, newFileName);
                db.SaveChanges();
                return Content(HttpStatusCode.Created, "Data was save");
            }
            catch (Exception ex)
            {
                return Content(HttpStatusCode.BadRequest, ex);
            }                    

        }
        catch (Exception ex)
        {
            return Content(HttpStatusCode.BadRequest, ex);
        }
    }

如果有人有更好的方法可以做到这一点,我仍然很高兴了解它。 谢谢。