0

I have to download zipped image from image URL path. Downloading image is clear. But problem is that prompt window is not appearing i.e. FileStreamResult returns nothing.

Asp.NET MVC controller post method:

[AcceptVerbs(HttpVerbs.Post)]
public FileResult Extract(string[] name)
{   
    using (ZipFile zip = new ZipFile()) //Zip section
    {
        foreach (var item in name)
        {
            string exts = Path.GetExtension(item);
            string strRealname = Path.GetFileName(item);

            using (WebClient client = new WebClient()) // download Section
            {
                client.DownloadFile(item, Server.MapPath("~/upload/") + strRealname + exts);
            }

            string filePath = Server.MapPath("~/upload/" + strRealname + exts);
            zip.AddFile(filePath, Path.GetFileName(filePath));
        }

        string dest = Server.MapPath("~/Album.zip");
        zip.Save(dest);
        byte[] data = System.IO.File.ReadAllBytes(dest);
        var strm = new FileStream(dest, FileMode.Open);
        //return new FileStreamResult(strm, "application/zip") 
        return new FileContentResult(data, "application/zip");//Not showing prompt window
    }
}

My problem is that return FileStreamResult is not working - why? I have tried too much but not succedded. The values of string[] name is obtained by using Ajax\JqueryUI post method .Actually the values are checkBox's value those are checked .

user2536026
  • 3
  • 1
  • 3
  • You can't download a file via ajax, because ajax is asynchronous.. and the user has to allow the download to proceed. As such, you must open a new window if you want the request to happen without redirecting the current window. – Erik Funkenbusch Jun 30 '13 at 09:28
  • I just told you how, you have to open a new window with the URL of the file to download. – Erik Funkenbusch Jun 30 '13 at 10:04
  • why files can not be download by ajax request?please describe me the mechanism behind that – user2536026 Jun 30 '13 at 11:01
  • @Mystere can you tell me how can i post values of checked box only to the controller without using Ajax/JQuey in asp.net mvc 3 – user2536026 Jun 30 '13 at 11:16
  • 1
    You wouldn't normally post anything, you would use a get and pass them as querystring parameters. The fact is, you simply cannot download via ajax. This is largely for security reasons, and there is no way around it. You will have to use alternate means. – Erik Funkenbusch Jun 30 '13 at 11:34
  • Totally agree with @MysterMan. First you don't do a POST to get a file back; second you can't get a file through an AJAX request, this answer fits yours well : http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – DJ. Jul 01 '13 at 02:04

1 Answers1

0

This is how i loaded images into an img tag.

Using C# mvc. ImageHelper is my own little helper, names should be explanatory. Loading it from a byte array(varbinary(max)) in a sql database.

 public JsonResult GetLogoImage2(){
    var image = ImageHelper.ByteArrayToImage(SomeDataBaseEntity.EssLogoImageAsBytes); 
    var png = ImageHelper.ConvertToPng(image);
    return Json(new { ImgBase64 = Convert.ToBase64String(ImageHelper.ImageToByteArray(png)) });
}

The original image load, i set like this

Javascript after an event to reload image.

$.post("/Account/GetLogoImage2", {}, function (result) {
  if (result.ImgBase64) {
     $('#logo_image').html('<img src="data:image/png;base64,' + result.ImgBase64 + '" />');
  }
}, 'json');

Yes, i have GetLogoImage and a GetLogoImage2 function. Working on cleaning up GetLogoImage, this was older code.

Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42