使用 FileResult 保存文件并提示 'Save as...'
Save file with prompt 'Save as...' using FileResult
我需要在点击某些文本时下载特定文件。我希望典型的 'Save as..' 对话框可以选择我要保存文件的位置,但它没有出现。请求和响应都OK。
Request/Response header
GET /Survey/GetSurveyFile?survey=1085&surveyFileType=2 HTTP/1.1
Host: localhost:50518
Connection: keep-alive
User-Agent: 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
Accept: /
X-Requested-With: XMLHttpRequest
======================
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/octet-stream
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 5.2
Content-Disposition: attachment; filename="1052__1183__1291__Variable Header Definition.txt"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?UzpcVlNTb3VyY2VcUHJvamVrdGVcTU1JXGJmdWVudGVzXE1NSVxNaW5kc2hhcmUuTU1JXE1NSVxTdXJ2ZXlcR2V0U3VydmV5RmlsZQ==?=
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Mon, 17 Aug 2015 14:21:48 GMT
Content-Length: 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;
}
}
知道如何获得所需的行为吗?
原始(阅读之后的更新部分)
看这里download file using an ajax request
我已经在我的机器上尝试了下一个代码
function getfile() {
$.ajax({
type: 'get',
url: '@Url.Action("Download")',
success: function () {
window.location = '@Url.Action("Download")';
}
});
}
$(function() {
$('h2').on('click', getfile);
});
public FileResult Download()
{
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "123.txt");
}
更新(v2)
您不需要ajax请求。更改 window.location 就足够了:
function getfile() {
var p1 = Math.random().toString();
var p2 = Math.floor(Math.random() * 100);
window.location = '@Url.Action("Download")?' + 'p1=' + p1 + '&' + 'p2=' + p2;
}
$(function() {
$('h2').on('click', getfile);
});
public FileResult Download(string p1, int p2)
{
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, string.Format("123_{0}_{1}.txt", p1, p2));
}
我需要在点击某些文本时下载特定文件。我希望典型的 'Save as..' 对话框可以选择我要保存文件的位置,但它没有出现。请求和响应都OK。
Request/Response header
GET /Survey/GetSurveyFile?survey=1085&surveyFileType=2 HTTP/1.1
Host: localhost:50518
Connection: keep-alive
User-Agent: 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
Accept: /
X-Requested-With: XMLHttpRequest
======================
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/octet-stream
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 5.2
Content-Disposition: attachment; filename="1052__1183__1291__Variable Header Definition.txt"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?UzpcVlNTb3VyY2VcUHJvamVrdGVcTU1JXGJmdWVudGVzXE1NSVxNaW5kc2hhcmUuTU1JXE1NSVxTdXJ2ZXlcR2V0U3VydmV5RmlsZQ==?=
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Mon, 17 Aug 2015 14:21:48 GMT
Content-Length: 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;
}
}
知道如何获得所需的行为吗?
原始(阅读之后的更新部分)
看这里download file using an ajax request
我已经在我的机器上尝试了下一个代码
function getfile() {
$.ajax({
type: 'get',
url: '@Url.Action("Download")',
success: function () {
window.location = '@Url.Action("Download")';
}
});
}
$(function() {
$('h2').on('click', getfile);
});
public FileResult Download()
{
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "123.txt");
}
更新(v2)
您不需要ajax请求。更改 window.location 就足够了:
function getfile() {
var p1 = Math.random().toString();
var p2 = Math.floor(Math.random() * 100);
window.location = '@Url.Action("Download")?' + 'p1=' + p1 + '&' + 'p2=' + p2;
}
$(function() {
$('h2').on('click', getfile);
});
public FileResult Download(string p1, int p2)
{
var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, string.Format("123_{0}_{1}.txt", p1, p2));
}