在我的ApiController类中,我有以下方法来下载服务器创建的文件.
public HttpResponseMessage Get(int id)
{
try
{
string dir = HttpContext.Current.Server.MapPath("~"); //location of the template file
Stream file = new MemoryStream();
Stream result = _service.GetMyForm(id, dir, file);
if (result == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
result.Position = 0;
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
return response;
}
catch (IOException)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
除了默认下载文件名是其id之外,一切都工作正常,因此用户可能每次都需要在另存为对话框时键入他/她自己的文件名.有没有办法在上面的代码中设置默认文件名?
我在网站上有一个部分,我在灯箱里面显示一个pdf.最近的Chrome升级打破了这个显示:
错误349(net :: ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION):收到多个Content-Disposition标头.这是不允许的,以防止HTTP响应分裂攻击.
这仍然可以在IE中正常工作.
我在IIS6上使用ASP.NET MVC3
我用来生成文件的代码如下.
如果我删除内联语句,则文件会下载,但会破坏灯箱功能.
问题代码
public FileResult PrintServices()
{
//... unrelated code removed
MemoryStream memoryStream = new MemoryStream();
pdfRenderer.PdfDocument.Save(memoryStream);
string filename = "ServicesSummary.pdf";
Response.AppendHeader("Content-Disposition", "inline;");
return File(memoryStream.ToArray(), "application/pdf", filename);
}
Run Code Online (Sandbox Code Playgroud)
修复
去掉
Response.AppendHeader("Content-Disposition", "inline;");
Run Code Online (Sandbox Code Playgroud)
然后改变
return File(memoryStream.ToArray(), "application/pdf", filename);
Run Code Online (Sandbox Code Playgroud)
至
return File(memoryStream.ToArray(), "application/pdf");
Run Code Online (Sandbox Code Playgroud) 我有一个在Windows 8.1,.net 4.5.1,IIS 8.5(在集成AppPool下),Visual Studio 2013上开发的Web应用程序,在默认模板上包含asp.net Identity,Owin等,并且本地工作正常.
然后我将它上传到Windows Server 2008和IIS 7.5(集成管道)主机,我得到:
此操作需要IIS集成管道模式.
异常详细信息: System.PlatformNotSupportedException:此操作需要IIS集成管道模式.
堆栈跟踪:
[PlatformNotSupportedException:此操作需要IIS集成管道模式.] System.Web.HttpResponse.get_Headers()+ 9687046 System.Web.HttpResponseWrapper.get_Headers()+9 Microsoft.Owin.Host.SystemWeb.OwinCallContext.CreateEnvironment()+309 Microsoft .Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.GetInitialEnvironment(HttpApplication application)+246 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.PrepareInitialContext(HttpApplication application)+15 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.BeginEvent (Object sender,EventArgs e,AsyncCallback cb,Object extradata)+265 System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+285 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&completedSynchronously)+155
我已经搜索了很多,但除了指示读者将管道从经典模式更改为集成模式之外我无法找到任何内容,而我已经没有运气了.
我该怎么做才能解决问题?"Microsoft.Owin.Host.SystemWeb"不是像IIS 7.5,还是服务器2008或我:)?
我目前正在开发一个PHP脚本,允许您通过访问链接从移动设备下载媒体内容(视频,音频,图片...).(即http://www.my-web-site.com/download.php?id=7ejs8ap)当我用最近的手机(三星Galaxy S,iPhone 4S,其他一些人)测试时,我的脚本非常有用. )但我的旧手机三星C3050出现错误.我想下载的媒体只是一个音频mp3文件,我通常很容易下载.
该错误似乎是"未知内容类型".因此,由于我唯一的HTTP标头Content-Type是"application/force-download",我尝试对此进行评论并再试一次.然后,它的工作原理.但是现在,我目前正在询问这种内容类型的含义以及是否可以强制其他移动设备.我在iPhone 4上没有使用Content-Type进行测试,但它确实有效,但我不确定所有移动设备的兼容性.
有人可以解释一下Content-Type是如何工作的,为什么这不是标准的MIME或其他所有可以帮助我确保这是每个下载的选项内容类型,无论文件,浏览器或设备是什么我正在下载?
感谢大家.
这是我发送的PHP标头:
<?php
//Assume that $filename and $filePath are correclty set.
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$filename.'"');
// header('Content-Type: application/force-download'); Non-standard MIME-Type, incompatible with Samsung C3050 for example. Let it commented
readfile($filePath);
?>
Run Code Online (Sandbox Code Playgroud)
编辑:我刚试过索尼Xperia,下载不成功:我只看到我要下载的文件的"html编码"字节.如果application/octet-stream或application/force-download不起作用,我怎么知道我必须使用哪种内容类型?
我不想在浏览器窗口中显示PNG,而是希望操作结果触发文件下载对话框(您知道打开,另存为等).我可以使用未知的内容类型来使用下面的代码,但是用户必须在文件名的末尾键入.png.如何在不强制用户输入文件扩展名的情况下完成此行为?
public ActionResult DownloadAdTemplate(string pathCode)
{
var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
return base.File(imgPath, "application/unknown");
}
Run Code Online (Sandbox Code Playgroud)
public ActionResult DownloadAdTemplate(string pathCode)
{
var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
Response.AddHeader("Content-Disposition", "attachment;filename=DealerAdTemplate.png");
Response.WriteFile(imgPath);
Response.End();
return null;
}
Run Code Online (Sandbox Code Playgroud) 我想通过在MVC下使用jQuery AJAX调用和一些参数来提供文件下载操作
例
(javascript)
function DoDownload(startDate) {
$.ajax({
url:"controller/GetFile/",
data: {startDate:startDate}
...
});
}
C# Controller Code
public void GetFile(string startDate) {
var results = doQueryWith(startDate);
// Create file based on results
....
// How do I tell the server to make this a file download??
}
Run Code Online (Sandbox Code Playgroud)
我通常会让我的文件下载一个链接,如:
<a h r e f="mycontroller/getfile/1"/>Download</a>
Run Code Online (Sandbox Code Playgroud)
但在上面的情况下,日期将是动态的.
如果我不使用ajax,使用javascript将params传递给MVC控制器的首选方法是什么?
例:
window.location = "mycontroller/GetFile/" + $("#fromDate").val();
Run Code Online (Sandbox Code Playgroud)
假设日期是12-25-2012
这会产生吗?
mycontroller/GetFile/12/25/2012
Run Code Online (Sandbox Code Playgroud)
MVC会将此视为三个参数吗?
我正在将数据从服务器传输到客户端以供下载使用filestream.write.在这种情况下,发生的事情是我能够下载该文件,但它不会在我的浏览器中显示为下载."另存为"弹出窗口不会出现在"下载"部分中的"下载栏"中.从四处查看,我想我需要在响应标题中包含"something"来告诉浏览器这个响应有一个附件.我也想设置cookie.要做到这一点,这就是我在做的事情:
[HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" & name)]
public ActionResult Download(string name)
{
// some more code to get data in inputstream.
using (FileStream fs = System.IO.File.OpenWrite(TargetFile))
{
byte[] buffer = new byte[SegmentSize];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, SegmentSize)) > 0)
{
fs.WriteAsync(buffer, 0, bytesRead);
}
}
}
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:"System.web.httpcontext.current是一个属性,用作类型."
我在正确的位置更新标题吗?有没有其他方法可以做到这一点?
我目前正在使用MVC4中的RazorPDF组装和显示PDF,并希望在返回视图的同时将PDF文件保存到文件系统.
控制器操作中的以下代码行调用视图:
return new PdfResult(claims, "PDF");
Run Code Online (Sandbox Code Playgroud) 我正在尝试csv使用打开/保存选项将文件导出到用户.
我的问题是类似于how-to-force-chrome-to-open-an-open-file-dialog-when-download-a-file-via-as(It is downloading the file in Chrome and Firefox),我试过用@Dev它提出的解决方案,但它是不工作
我编写了如下代码: -
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv", filename);
Run Code Online (Sandbox Code Playgroud)
但是,它无法在Chrome中运行.默认情况下会下载该文件.
然后在谷歌搜索后,我发现返回文件到视图下载在mvc,我试图做以下的事情: -
var csvData = "hello";// I am filling this variable with ,y values from DB!
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = "test",
Inline = false,
};
Response.AppendHeader("Content-Disposition",
cd.ToString());
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv");
Run Code Online (Sandbox Code Playgroud)
但它仍然是在Chrome中下载文件.然后我遇到了如何显示 - 打开 - 保存 - 对话框 - asp-net-mvc-4,其中@JoãoSimões提到: - …
我正在研究以CSV格式导出数据的机制.我JSON使用jQuery以格式发送数据:
var data = JSON.stringify(dataToSend);
$.post('DumpToCSV', { 'data': data });
Run Code Online (Sandbox Code Playgroud)
然后在控制器中我生成一个CSV文件:
public ActionResult DumpToCSV(string data)
{
Response.Clear();
XmlNode xml = JsonConvert.DeserializeXmlNode("{records:{record:" + data + "}}");
XmlDocument xmldoc = new XmlDocument();
//Create XmlDoc Object
xmldoc.LoadXml(xml.InnerXml);
//Create XML Steam
var xmlReader = new XmlNodeReader(xmldoc);
DataSet dataSet = new DataSet();
//Load Dataset with Xml
dataSet.ReadXml(xmlReader);
//return single table inside of dataset
var csv = CustomReportBusinessModel.ToCSV(dataSet.Tables[0], ",");
HttpContext context = System.Web.HttpContext.Current;
context.Response.Write(csv);
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
c# ×4
download ×3
ajax ×2
asp.net ×2
content-type ×1
controller ×1
csv ×1
http ×1
http-headers ×1
httpcontext ×1
iis ×1
iis-6 ×1
image ×1
jquery ×1
json ×1
mobile ×1
php ×1
razorpdf ×1
response ×1