【发布时间】:2015-10-21 19:53:05
【问题描述】:
我想我在 JavaScript 方面遗漏了一些代码。我正在下载每个请求的文件。当用户单击链接时,我会获取文档数据并将其流式传输。我在 Fiddler 上看到数据正在下降,但 .txt 文档链接未打开。
[HttpGet]
public HttpResponseMessage GetDataFiles(Int64 Id)
{
var results = context.PT_MVC_RequestFile.Where(x => x.RowId == Id).FirstOrDefault();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
try
{
if (results != null)
{
response.Headers.AcceptRanges.Add("bytes");
response.StatusCode = HttpStatusCode.OK;
response.Content = new ByteArrayContent(results.Data);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = results.FileName;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength = results.Data.Length;
}
}
catch (EntityException ex)
{
throw new EntityException("GetFiles Failed" + ex.Message);
}
return response;
}
首先,我下载了该请求的所有文档,如果用户单击该文件,我将调用下载流操作。
$.ajax({
url: url,
type: 'GET',
// data: JSON.stringify(model, null),
contentType: "application/json",
success: function (data) {
if (data != "") {
$("#fileLength").val(data.length);
// alert(data.length);
$.each(data, function (i, item) {
var newDiv = $(document.createElement('div')).attr("id", 'file' + i);
newDiv.html("<input id=\"cb" + i + "\" type=\"checkbox\"> <a href=\"#\" onclick=\"GetData('" + item.RowId + "','" + item.mineType + "')\" >" + item.FileName + "</a>");
newDiv.appendTo("#fileRows");
});
} else {
}
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
我认为我在成功后遗漏了一些东西。不知何故,它下载了数据,但链接没有打开。可能是没有设置内容类型,或者它认为它是 JSON 数据?请提供一些想法。
这是链接:
function GetData(rowId,mineType) {
// alert(mineType);
var url = "api/MyItemsApi/GetDataFiles/" + rowId;
$.ajax({
url: url,
type: 'GET',
//data: JSON.stringify(model, null),
contentType: "application/json",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
}
【问题讨论】:
-
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 6429 Content-Type: application/txt Expires: -1 Accept-Ranges: bytes 服务器: Microsoft- IIS/8.0 内容处置:附件;文件名= Bare.txt X-ASPNET-版本:4.0.30319 X-SourceFiles:= UTF-8乙QzpcVXNlcnNcYzBhYmRnZVxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEyXFByb2plY3RzXFByb2plY3QuVHJhY2tlci5XZWIuU29sdXRpb25cUHJvamVjdC5UcmFja2VyLldlYlxhcGlcTXlJdGVtc0FwaVxHZXREYXRhRmlsZXNcMQ == = X供电-通过:????ASP.NET日期:星期四,2015年7月30日13时33分:46 GMT 这是标题中的内容,我将其更改为 txt 但仍然没有运气
标签: javascript c# jquery asp.net-web-api