ASP.NET Web API 2文件上传

编程入门 行业动态 更新时间:2024-10-17 19:25:57
本文介绍了ASP.NET Web API 2文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何最好地使用ASP.NET Web API 2在没有MVC组件的情况下处理文件上载以及添加到要上载的文件中的其他信息.我有Google网,可以告诉您我比预期的更加困惑.

I would like to know how best to handle file upload and addtional information added to the file to be uploaded using ASP.NET Web API 2 without MVC components. I have google the net and I can tell you I am more confused than I expected.

其他信息将存储在db中,文件将存储在磁盘上. 到目前为止,我正在构建的Web API应用程序不支持multipart/form-data.它仅支持默认媒体类型.我知道我需要创建一个媒体格式化程序.

The Additional info will be stored in db and the file on the disk. So far the Web API app I am building does not support multipart/form-data. It only supports the default media types. I know I need to create a media formatter.

请帮助.

推荐答案

我写了Javascript split File并上传到WEB API.我认为您可以引用我的后端代码

I had wrote Javascript split File and upload to WEB API . i think you can reference my backend codes

在前端,您需要使用以下代码上传文件

In front-end you need using below code to upload your File

var xhr = new self.XMLHttpRequest(); xhr.open('POST', url, false); xhr.setRequestHeader('Content-Type', 'application/octet-stream'); xhr.send(chunk);

在后端使用Request.InputStream.Read捕获文件字节

In backend use Request.InputStream.Read to catch your file bytes

[HttpPost] [ValidateInput(false)] public string fileUpload(string filename) { byte[] file = new byte[Request.InputStream.Length]; Request.InputStream.Read(file, 0, Convert.ToInt32(Request.InputStream.Length)); BinaryWriter binWriter = new BinaryWriter(new MemoryStream()); binWriter.Write(file); StreamReader reader = new StreamReader(binWriter.BaseStream); reader.BaseStream.Position = 0; //This example is recevied text file while ((line = reader.ReadLine()) != null) { }; }

更多推荐

ASP.NET Web API 2文件上传

本文发布于:2023-11-14 04:14:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件上传   NET   ASP   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!