gpt4 book ai didi

javascript - 使用 FileResult 保存文件并提示 'Save as...'

转载 作者:行者123 更新时间:2023-12-02 15:47:53 26 4
gpt4 key购买 nike

我需要在单击某些文本时下载特定文件。我希望典型的“另存为..”对话框可以选择保存文件的位置,但它没有出现。请求和响应都正常。

请求/响应 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/"
}
});
}

Controller

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));
}

关于javascript - 使用 FileResult 保存文件并提示 'Save as...',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com