Rather than displaying a PNG in the browser window, I'd like the action result to trigger the file download dialogue box (you know the open, save as, etc). I can get this to work with the code below using an unknown content type, but the user then has to type in .png at the end of the file name. How can I accomplish this behavior without forcing the user to type in the file extension?
我不希望在浏览器窗口中显示PNG,而是希望操作结果触发文件下载对话框(您知道open、save as等)。我可以使用下面的代码使用一个未知的内容类型,但是用户必须在文件名后面输入.png。如何在不强制用户输入文件扩展名的情况下完成此行为?
public ActionResult DownloadAdTemplate(string pathCode)
{
var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
return base.File(imgPath, "application/unknown");
}
Solution....
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;
}
6 个解决方案
#1
42
I believe you can control this with the content-disposition header.
我相信您可以通过内容配置头来控制这个。
Response.AddHeader(
"Content-Disposition", "attachment; filename=\"filenamehere.png\"");
#2
9
You need to set the following headers on the response:
您需要在响应上设置以下标题:
Content-Disposition: attachment; filename="myfile.png"
- 附加:附件;文件名= " myfile.png "
Content-Type: application/force-download
- 内容类型:应用程序/ force-download
#3
5
I actually came here because I was looking for the opposite effect.
我来这里是因为我想找到相反的效果。
public ActionResult ViewFile()
{
string contentType = "Image/jpeg";
byte[] data = this.FileServer("FileLocation");
if (data == null)
{
return this.Content("No picture for this program.");
}
return File(data, contentType, img + ".jpg");
}
#4
3
With MVC I use a FileResult and return a FilePathResult
使用MVC,我使用FileResult并返回FilePathResult
public FileResult ImageDownload(int id)
{
var image = context.Images.Find(id);
var imgPath = Server.MapPath(image.FilePath);
return File(imgPath, "image/jpeg", image.FileName);
}
#5
1
This I actually @7072k3
这实际上我@7072k3
var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;
Copied that from my working code. This still uses the standard ActionResult return type.
从我的工作代码中复制它。这仍然使用标准的ActionResult返回类型。
#6
1
The correct way to download file in your case is to use FileResult
class.
下载文件的正确方法是使用FileResult类。
public FileResult DownloadFile(string id)
{
try
{
byte[] imageBytes = ANY IMAGE SOURCE (PNG)
MemoryStream ms = new MemoryStream(imageBytes);
var image = System.Drawing.Image.FromStream(ms);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var fileName = string.Format("{0}.png", "ANY GENERIC FILE NAME");
return File(ms.ToArray(), "image/png", fileName);
}
catch (Exception)
{
}
return null;
}
#1
42
I believe you can control this with the content-disposition header.
我相信您可以通过内容配置头来控制这个。
Response.AddHeader(
"Content-Disposition", "attachment; filename=\"filenamehere.png\"");
#2
9
You need to set the following headers on the response:
您需要在响应上设置以下标题:
Content-Disposition: attachment; filename="myfile.png"
- 附加:附件;文件名= " myfile.png "
Content-Type: application/force-download
- 内容类型:应用程序/ force-download
#3
5
I actually came here because I was looking for the opposite effect.
我来这里是因为我想找到相反的效果。
public ActionResult ViewFile()
{
string contentType = "Image/jpeg";
byte[] data = this.FileServer("FileLocation");
if (data == null)
{
return this.Content("No picture for this program.");
}
return File(data, contentType, img + ".jpg");
}
#4
3
With MVC I use a FileResult and return a FilePathResult
使用MVC,我使用FileResult并返回FilePathResult
public FileResult ImageDownload(int id)
{
var image = context.Images.Find(id);
var imgPath = Server.MapPath(image.FilePath);
return File(imgPath, "image/jpeg", image.FileName);
}
#5
1
This I actually @7072k3
这实际上我@7072k3
var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;
Copied that from my working code. This still uses the standard ActionResult return type.
从我的工作代码中复制它。这仍然使用标准的ActionResult返回类型。
#6
1
The correct way to download file in your case is to use FileResult
class.
下载文件的正确方法是使用FileResult类。
public FileResult DownloadFile(string id)
{
try
{
byte[] imageBytes = ANY IMAGE SOURCE (PNG)
MemoryStream ms = new MemoryStream(imageBytes);
var image = System.Drawing.Image.FromStream(ms);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var fileName = string.Format("{0}.png", "ANY GENERIC FILE NAME");
return File(ms.ToArray(), "image/png", fileName);
}
catch (Exception)
{
}
return null;
}