ASP.NET MVC pobiera obraz zamiast wyświetlać w przeglądarce

Zamiast wyświetlać PNG w oknie przeglądarki, chciałbym, aby wynik działania wywołał okno dialogowe pobierania pliku (znasz Otwórz, Zapisz jako, itp.). Mogę to zrobić z poniższym kodem, używając nieznanego typu treści, ale użytkownik musi wpisać .png na końcu nazwy pliku. Jak mogę osiągnąć to zachowanie bez zmuszania użytkownika do wpisania rozszerzenia pliku?

    public ActionResult DownloadAdTemplate(string pathCode)
    {
        var imgPath = Server.MapPath(service.GetTemplatePath(pathCode));
        return base.File(imgPath, "application/unknown");
    }

Rozwiązanie....

    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;
    }
Author: RSolberg, 2010-06-09

6 answers

Wierzę, że możesz to kontrolować za pomocą nagłówka Content-disposition.

Response.AddHeader(
       "Content-Disposition", "attachment; filename=\"filenamehere.png\""); 
 42
Author: womp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-09 16:18:52

Musisz ustawić następujące nagłówki odpowiedzi:

  • Content-Disposition: attachment; filename="myfile.png"
  • Content-Type: application/force-download
 9
Author: Aren,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-09 16:10:13

Przyszłam tu, bo szukałam odwróconego efektu.

    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");
    }
 5
Author: Paul Totzke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-28 13:15:11

Z MVC używam FileResult i zwracam 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);
    }
 3
Author: Iain M Norman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-16 09:28:01

This I actually @ 7072k3

var result = File(path, mimeType, fileName);
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "inline");
return result;
Skopiowałem to z mojego kodu roboczego. W dalszym ciągu używa się standardowego typu zwracania ActionResult.
 1
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-24 01:28:03

Poprawnym sposobem pobrania pliku w Twoim przypadku jest użycie klasy 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
Author: Academy of Programmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-09 17:02:07