Botón de descarga para descargar de la biblioteca de imágenes

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.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";

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?

preguntado el 08 de noviembre de 11 a las 16:11

Sure I can! If I use an absolute path like 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'. -

fileInfo.FullName is "c:\myPictureLibrary\pic.jpg", I wrote that wrong on the previous comment! -

No puedes usar el FileInfo class to check if a file exists in a SharePoint library! Utilizan SPFile en su lugar. . -

1 Respuestas

Creo que deberías usar 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() algo como esto:

var wc=new System.Net.WebClient();
var data=wc.DownloadData(url);
//add headers..

respondido 09 nov., 11:07

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

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

@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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.