我想知道如何最好地处理文件上传以及添加到要使用 ASP.NET Web API 2 上传的文件而不使用 MVC 组件的附加信息。我用谷歌搜索了网络,我可以告诉你我比我想象的更困惑。
附加信息将存储在数据库和磁盘上的文件中。
到目前为止,我正在构建的 Web API 应用程序不支持 multipart/form-data。它仅支持默认媒体类型。我知道我需要创建一个媒体格式化程序。
请帮忙。
我编写了 Javascript split File 并上传到 WEB API 。我想你可以参考我的后端代码
在前端,您需要使用以下代码来上传文件
var xhr = new self.XMLHttpRequest();
xhr.open('POST', url, false);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(chunk);
在后端使用 Request.InputStream.Read 来捕获文件字节
[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)
{
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)