如何通过JSON对象将文件流上传到ASP.NET中的Web API 2? [英] How do I upload a file stream to the web API 2 in ASP.NET via JSON object?

查看:90
本文介绍了如何通过JSON对象将文件流上传到ASP.NET中的Web API 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,

我有一个场景,我想通过JSON对象将文件流从客户端(我使用AngularJS)上传到asp .net中的Web Api。 />


因此JSON对象中会有一些字段,其中一个字段是文件流。

如何创建JSON对象并将其发送到web api将模型绑定到api端到我的模型。



Hello All,
I have a scenario in which i want to upload the file stream from client side (I am using AngularJS) to the Web Api in asp .net via JSON object.

So there will be some fields in JSON object in which one field is file stream.
How do i create a JSON object and send it to the web api which get model bind at api side to my model.

$scope.fileUploadData = {
        TextId: 304765,
        DocumentId: 0,
        PatientId: 166158,
        file://This is my file stream property in JSON
}




public ApiActionResult CreateDocument(DocumentVM model)
{
  //Some logic here.....
}

// DocumentVM class will be as follows
public class DocumentVM
{
        long TextId;
        long DocumentId;
        long PatientId;
        Stream file;
}





请提供示例代码片段如何实现它。



我尝试了什么:



我搜索了很多但是我找到了使用多部分表单数据的解决方案。在其中附加JSON和文件流,然后将其发送到web api。我使用以下逻辑来提取多部分表单数据。



Please provide me the sample code snippets how to achieve it.

What I have tried:

I googled lots but i found the solutions like using multipart form data. In which JSON and file stream appended and then send it to the web api. I used following logic to extract the multi part form data.

public async Task<ApiActionResult> CreateDocument()
{
  MultipartFormDataStreamProvider provider = 
            new MultipartFormDataStreamProvider(Path.GetTempPath());
            var result = await Request.Content.ReadAsMultipartAsync(provider);
            var model= result.FormData["Metadata"];
            var fileData = result.FileData;//How it will retriev the file data
}

推荐答案

scope.fileUploadData = {
TextId: 304765
DocumentId: 0
PatientId: 166158
file: // 这是我的文件流属性in JSON
}
scope.fileUploadData = { TextId: 304765, DocumentId: 0, PatientId: 166158, file://This is my file stream property in JSON }




public ApiActionResult CreateDocument(DocumentVM model)
{
  //Some logic here.....
}

// DocumentVM class will be as follows
public class DocumentVM
{
        long TextId;
        long DocumentId;
        long PatientId;
        Stream file;
}





请提供示例代码片段如何实现它。



我尝试了什么:



我搜索了很多但是我找到了使用多部分表单数据的解决方案。在其中附加JSON和文件流,然后将其发送到web api。我使用以下逻辑来提取多部分表单数据。



Please provide me the sample code snippets how to achieve it.

What I have tried:

I googled lots but i found the solutions like using multipart form data. In which JSON and file stream appended and then send it to the web api. I used following logic to extract the multi part form data.

public async Task<ApiActionResult> CreateDocument()
{
  MultipartFormDataStreamProvider provider = 
            new MultipartFormDataStreamProvider(Path.GetTempPath());
            var result = await Request.Content.ReadAsMultipartAsync(provider);
            var model= result.FormData["Metadata"];
            var fileData = result.FileData;//How it will retriev the file data
}


这篇关于如何通过JSON对象将文件流上传到ASP.NET中的Web API 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆