开发者

ASP.Net HyperLink and Download directly?

want to have a Hyperlink-Button in a gridView in which I can display a 开发者_C百科Link for a direct download of files.

So far I am okay, but when I link the file in the NavigateURL, I don't get the file via click, I must right click -> Save as! That I don't want. Any help?


You could set up an ashx file handler. Your ashx takes the request through the querystring, loads the proper file into memory, sets the proper response headers and streams the file to the browser:

FileInfo fileInfo = new FileInfo(PATH-TO-YOUR-FILE);  //You need to specify this
context.Response.ContentType = YOUR-CONTENT-TYPE;  //And this
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.WriteFile(fileInfo.FullName);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest()

This lets you have some fine-grain control over the content that is being delivered if you have any concerns about security or maybe keeping track of who has downloaded what file, etc.


This sounds like a browser issue. Possibly the browser is trying to open up some application to handle this and failing. Double-check your application associations and/or try a new browser.


I would add also "Content-Disposition" header to response:

context.Response.AppendHeader("Content-Disposition", "attachment; filename = " + filename);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜