单击某些文本时,我需要下载特定文件。我希望典型的“另存为..”对话框可以选择我要保存文件的位置,但它没有出现。请求和响应都OK。
请求/响应标头
GET /Survey/GetSurveyFile?survey=1085&surveyFileType=2 HTTP/1.1
主机:本地主机:50518
连接:保持活动
用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36 OPR/31.0.1889.99
接受:/
X-Requested-With: XMLHttpRequest
=======================
HTTP/1.1 200 正常
缓存控制:私有
内容类型:应用程序/八位字节流
服务器:Microsoft-IIS/8.0
X-AspNetMvc-版本:5.2
内容处置:附件;文件名="1052__1183__1291__变量头定义.txt"
X-AspNet-版本:4.0.30319
X-SourceFiles: =?UTF-8?B?UzpcVlNTb3VyY2VcUHJvamVrdGVcTU1JXGJmdWVudGVzXE1NSVxNaW5kc2hhcmUuTU1JXE1NSVxTdXJ2ZXlcR2V0U3VydmV5RmlsZQ==?=
持久认证:真
X-Powered-By: ASP.NET
日期:2015 年 8 月 17 日星期一 14:21:48 GMT
内容长度:333
我的代码:
Javascript
function getfile(filetype) {
var SurveyId = $('#SurveyID').val();
var url = '/Survey/GetSurveyFile';
$.ajax({
type: 'GET',
url: url,
data: { survey: SurveyId, surveyFileType: filetype },
success: function (result) {
// ?
},
error: function (result) {
// handle errors
location.href = "/Home/"
}
});
}
控制器
public FileResult GetSurveyFile(string survey, string surveyFileType)
{
try
{
var tmpSurvey = EntityModelDataProvider.GetSurveyByID(int.Parse(survey));
var tmpSurveyFileTypes = EntityModelDataProvider.GetSurveyFileTypes();
var tmpSurveyFileType = tmpSurveyFileTypes.FirstOrDefault(_sft => _sft.SurveyFile_Type_Id == int.Parse(surveyFileType));
var tmpFile = EntityModelDataProvider.GetSurveyFilesBySurveyAndType(tmpSurvey.Survey_PK, tmpSurveyFileType.SurveyFile_Type_PK);
if (tmpFile != null)
{
byte[] fileBytes = tmpFile.SurveyFile;
string fileName = tmpFile.SurveyFile_Name;
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
else
throw new Exception("File not found!");
}
catch (Exception ex)
{
throw ex;
}
}
知道如何获得所需的行为吗?