开发者

how to make a picture file downloadable?

I have an 开发者_Go百科ASP.NET MVC3 application and I want to link_to an image file (png, jpeg, gif, etc), and when user clicks on it, the file goes to download, instead of the browser shows it; is there any way to do this?


take your link something like this:

@Html.ActionLink(
    "Download Image", // text to show
    "Download", // action name
    ["DownloadManager", // if need, controller]
    new { filename = "my-image", fileext = "jpeg" } // file-name and extension 
)

and action-method is here:

public FilePathResult Download(string filename, string fileext) {
    var basePath = Server.MapPath("~/Contents/Images/");
    var fullPath = System.IO.Path.Combine(
        basePath, string.Concat(filename.Trim(), '.', fileext.Trim()));
    var contentType = GetContentType(fileext);
    // The file name to use in the file-download dialog box that is displayed in the browser.
    var downloadName = "one-name-for-client-file." + fileext;
    return File(fullPath, contentType, downloadName);
}

private string GetContentType(string fileext) {
    switch (fileext) {
        case "jpg":
        case "jpe":
        case "jpeg": return "image/jpeg";
        case "png": return "image/x-png";
        case "gif": return "image/gif";
        default: throw new NotSupportedException();
    }
}

UPDATE: in fact, when a file is sending to a browser, this key/value will be generated in http-header:

Content-Disposition: attachment; filename=file-client-name.ext

which file-client-name.ext is the name.extension that you want the file save-as it on client system; for example, if you want to do this in ASP.NET (none mvc), you can create a HttpHandler, write the file-stream to Response, and just add the above key/value to the http-header:

Response.Headers.Add("Content-Disposition", "attachment; filename=" + "file-client-name.ext");

just this, enjoy :D


Well technically your browser is downloading it.

I don't think you can directly link to an image, and have the browser prompt to download.

You could try something where instead of linking directly to the image, you link to a page, which serves up the image in a zip file perhaps - which of course would prompt the download to occur.


Yes, you can.

Now, you'll need to customize this to suit your needs, but I created a FileController that returned files by an identifier (you can easily return by name).

public class FileController : Controller
{
    public ActionResult Download(string name)
    {
        // check the existence of the filename, and load it in to memory

        byte[] data = SomeFunctionToReadTheFile(name);
        FileContentResult result = new FileContentResult(data, "image/jpg"); // or whatever it is
        return result;
    }
}

Now, how you read that file or where you get it from is up to you. I then created a route like this:

 routes.MapRoute(null, "files/{name}", new { controller = "File", action = "Download"});

My database has a map of identifiers to files (it's actually more complex than this, but I am omitting that logic for brevity), I can write urls like:

 "~/files/somefile"

And the relevant file is downloaded.


I don't think this is possible but a simple message saying right click to save image would suffice I think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜