2014年7月13日 星期日

[RESOLVED] file download link within the gridview and perform download


hi,


 am having a gridview with path to the  file.name , where file is  physically stored in the filesystem and am storing the filepath in my db table column.so, i need to pull that path and show it in grdiview column. and when user clicks on the document link,
user shud be able to download the file .


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


or


should i store the file name and data/content in the  sql db itself as  varbinary(max) datatype.? and in that case, how to retrieve the content to download



Since it's just part of the filesystem, you can't link to it directly as you would need to have a virtual path, ie: a web-path and not a server-side path (as in: C:\myfiles\mydirectory\myfile.doc). If you use a LinkButton, you can set the commandargument
for it to be the file path, then for the command event try the following


string path = e.CommandArgument.ToString();
if(File.Exists(path))
{
Response.Clear();
Response.TransmitFile(path);
Response.Flush();
}

You can add custom logic for adding an appropriate content type so the browser will understand what to do with the file.





BenjaminKNR


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


Leave file in the filesystem.


Your files should be within your website directory


Suppose there is folder Name Files in your website



Set link in your gridview like "www.yoursite.com/Files/YourFile.txt" for files


Make sure permissions are set to read and download files from that folder


Regards




if (e.CommandName == "Download")
{
try
{
string filename = e.CommandArgument.ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Archiving.pdf");
Response.TransmitFile(Server.MapPath(filename ));
Response.End();
}
catch (Exception ex)
{
}
}

Im using LinkButton inside gridview where Command Argument =File Path 


Up Code For Download Any file .pdf and you can use Switch Statement for download any type


regards



If you want add download link column to gridview, save virtual path to filepath field. Virtual path like : filename.ext or folder/filename.ext or ~/folder/subfolder/filename.ext

沒有留言:

張貼留言