我想从服务器下载文件,但我不明白我做错了什么。我一直在寻找如何做,但不起作用。这是我发现的一个例子:
控制器(ASP NET MVC):
$scope.downloadFiles = function () {
var filename = "aae49c8e-c523-4ccc-a7ba-88f405072047&file.pdf";
$http({
method: 'GET',
url: 'serv/Consultas/GetFile',
params: { filename: filename },
responseType: "arraybuffer"
}).success(function (response) {
var file = new Blob([(response)], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
$window.open(fileURL);
}).error(function (data, status) {
console.log("Request failed with status: " + status);
});
}
角度控制器:
<img/>
当我加载文件时,我的文件名不完整&#34; aae49c8e-c523-4ccc-a7ba-88f405072047&#34;并且不加载该文件。谢谢你的帮助。
答案 0 :(得分:3)
从服务器流式传输文件:
public FileStreamResult GetFile(string filename)
{
try
{
if (!string.IsNullOrEmpty(filename))
{
//string filePath = HttpContext.Current.Server.MapPath("~/App_Data/") + fileName;
DirectoryInfo dirInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/Documentos"));
string filePath = dirInfo.FullName + @"\" + filename;
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/pdf");
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
catch (Exception)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
打开一个新窗口,其中包含URL操作方法,该方法将STREAM PDF以便在浏览器中显示:
var fileURL = 'serv/Consultas/GetFile?filename=file.pdf';
$window.open(fileURL);