0

I have a ListView with a picture library as source and for each picture I have a asp:LinkButton that represents a download button. When pressed my button should open the browser download box. I'm using the following code to achieve that:

public void Download_Click(object source, EventArgs e)
        {
LinkButton button = (LinkButton)source;
string url = Server.UrlEncode(button.CommandArgument);
FileInfo fileInfo = new FileInfo(url);

if (fileInfo.Exists)
            {      
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.WriteFile(fileInfo.FullName);
            }
            else
            {
                //error
            }
}

On that code I'm using Server.Encode("fileName") but I tried Server.Map and also used the "PicLibraryName/FileName" and "Application/PictureLibraryName/FileName" but I never get FileInfo.Exists true because I always get FileNotFoundException or System.Web.HttpException (when I use a virtual path).

Anyone knows what's the best way to solve this problem?

Pedro Fonseca
  • 198
  • 1
  • 16
  • Sure I can! If I use an absolute path like http://myapplication/myPictureLibrary/pic.jpg I get a "System.ArgumentException: URI formats are not supported" on the new FileInfo(url) and using the relative path /myPictureLibrary/pic.jpg the new FileInfo works, I can get fileInfo.Name but the fileInfo.FullName is only "\myPictureLibrary\pic.jpg", fileInfo.Exits is false and when I try to access some file properties like fileInfo.length I get an System.IO.FileNotFoundException: Could not find file '/myPictureLibrary/pic.jpg'. – Pedro Fonseca Nov 09 '11 at 11:05
  • fileInfo.FullName is "c:\myPictureLibrary\pic.jpg", I wrote that wrong on the previous comment! – Pedro Fonseca Nov 09 '11 at 11:42
  • You cannot use the `FileInfo` class to check if a file exists in a SharePoint library! [Use `SPFile` instead](http://stackoverflow.com/q/359672/95). – Marek Grzenkowicz Nov 09 '11 at 13:05

1 Answers1

0

I think you should use WebClient.DownloadData(url) for downloading the file and then Response.OutputStream.Write method for output downloading data, also don't forget to end response using Response.End() something like this:

var wc=new System.Net.WebClient();
var data=wc.DownloadData(url);
Response.Clear();
//add headers..
Response.OutputStream.Write(data);
Response.End();
Alexander
  • 1,287
  • 1
  • 15
  • 34
  • I've tried this code and the result was the same as I explained to Marek Grzenkowicz on the comment before: FileInfo fileInfo = new FileInfo(url); var wc = new System.Net.WebClient(); var data = wc.DownloadData(url); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name); Response.OutputStream.Write(data,0,data.Length); Response.End(); – Pedro Fonseca Nov 09 '11 at 11:56
  • Yes, sure. Something like: http:// application:55555/PictureGallery/image.jpg (the extra space is just so that stack overflow doesn't show it as hyperlink) and I've tried also just: /PictureGallery/image.jpg – Pedro Fonseca Nov 09 '11 at 17:03
  • @Cristo you should not use FileInfo class at all because it designed to work with file system, not with url, also you can't use Server.Map because image stored in content database. i see only reason you using it is for filename in header, but you can figure it out just by simple replacing `filename=url.Replace(url_of_you_library,"")`, or you can use SPList and SPFile classes but is more complex – Alexander Nov 10 '11 at 02:30