将文件上传到服务器时,它很棒; 没有损坏的文件.然而,当我下载文件时(除了纯粹的txt:s,它们是woork),它们的大小会变大并变得腐败.经过大量的调查后,我不知道会出现什么问题.我只是将文件作为流写入响应并下载blob.
欢迎任何想法!
严重依赖此Thread解决方案; 使用AngularJS从ASP.NET Web API方法下载文件
现行守则如下;
的WebAPI:
[Route("GetFile")]
public HttpResponseMessage GetFile()
{
HttpResponseMessage result = null;
//Get file object here
try
{
IEnumerable<string> headerValues = Request.Headers.GetValues("fileID");
int key = Int32.Parse(headerValues.FirstOrDefault());
var fetchFile = db.FileRecords.Single(a => a.id == key);
var localFilePath = fetchFile.path + fetchFile.name;
if (!System.IO.File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
//I have used the x-filename header to send the filename. This is a custom header for convenience.
//You should …Run Code Online (Sandbox Code Playgroud)