Prompt browser to download file from browser in asp.net using c# - asp.net

To download mp3 file from 3rd party servers , using general code give
error of need virtual path to download..
using this code :
var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();
error comes that no virtual path set...i repeat i need to download from 3rd party servers with
full url

Related

Accept-Ranges doesn't appear in asp.net response

I wrote a simple asp.net page that simulates file download response. I added the following lines to adjust response:
Response.AddHeader("ETag", "\"" + _EncodedData + "\"");
Response.AddHeader("Last-Modified", lastUpdateTiemStamp);
Response.AddHeader("Accept-Ranges", "bytes");
//Set the ContentType
Response.ContentType = "application/octet-stream";
//Add the file name and attachment,
//which will force the open/cancel/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment;filename=" + dItem.FileName);
//Add the file size into the response header
Response.AddHeader("Content-Length", (endByte - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
My problem is that Accept-ranges doesn't apear in the response header, but when I change Accept-Ranges to Accept-Ranges1 this header(Accept-Ranges1) will receive a response header.
This problem only occurs when I am debugging the project in Visual Studio. When I am on IIS everything is ok

IIS/ASP file download different to original

Have the following code under IIS/ASP, with Chrome browser under Windows:
Response.ContentType = "application/exe"; //also tried application/octet-stream
Response.AddHeader("content-disposition", "attachment;filename=MyFile.exe");
Response.TransmitFile(Server.MapPath("~/MyFile.exe"));
However when I complete the download, the file I downloaded is a different size to the original (the downloaded file is larger), and the digital sig is missing. How do I fix?
This seems to do the trick:
FileInfo info = new FileInfo(Server.MapPath("~/MyFile.exe"));
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-length", info.Length.ToString());
Response.AddHeader("content-disposition", "attachment;filename=MyFile.exe");
Response.TransmitFile(Server.MapPath("~/MyFile.exe"));

How to add all the files in directory in ASP.NET and when i click on the link that file should get download?

I wanna to display all the files in a directory in ASP.NET. when i click on the list of files available in that it should get download to local or it should open to view?
How to achieve this ? i got all the files in directory but i don't know to make those files to download?
Thanks in advance.
In order to make the file downlodble you have to add for each file an href
eg
foreach(string fileName in Dir)
{
Response.Write("<a href='"+fileName+"'>file name...</a>");
}
ore you have to force download in such way
public static void ForceDownload(this HttpResponse Response, string virtualPath, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(virtualPath);
Response.ContentType = "";
Response.End();
}
One way to do this is to enable directory browsing in IIS.
For whatever reason, directory browsing also displays the web.config file for the directory. The following link discuses how to not show it: http://forums.iis.net/t/1149484.aspx/1

how to open a file by its full path?

I have a GridView which has following template field that holds the filename, when the user clicks the filename, I call window.open() to open the file. My question is I can only pass relative path to window.open(). I got an error if I use full path. Is there any way I can use full path to open a file? Thanks.
<asp:TemplateField HeaderText="FileName">
<ItemTemplate>
<asp:LinkButton ID="lnkFile" runat="server"
Text='<%# Eval("FileName")%>' OnClick="lnkFile_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Added: The actual location of the file is defined in web.config.
I have wrote following lnkFile_Click(). The old part will open a new window for the file, but I cannot pass fullpath of the file. The new part will let you have a choice to open or save the file. My question is, will this cause security issue?
protected void lnkFile_Click(object sender, System.EventArgs e)
{
string fileName = ConfigurationSettings.AppSettings["SPRAttachmentLocation"] + "\\SPR" + sprID + "\\" + ((LinkButton)sender).Text;
if (!File.Exists(fileName))
{
Exception ex = new Exception("Could not find the file, please contact your administrator.");
Response.Write("<p align=\"center\"><br /><br /><br /><br />There has been an Error: <strong>" + ex.Message + "</strong></p>\n");
return;
}
New:
byte[] bts = System.IO.File.ReadAllBytes(fileName);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
Old:
string newWindowUrl = "/SPR_Upload/SPR" + sprID + "/" + ((LinkButton)sender).Text;
string javaScript =
"<script type='text/javascript'>\n" +
"<!--\n" +
"window.open('" + newWindowUrl + "');\n" +
"// -->\n" +
"</script>\n";
Page.ClientScript.RegisterStartupScript(GetType(), "", javaScript);
}
Your question gives the impression that you think that you can open a file from the user's local computer. If that's the case, this is not possible.
Window.open expects a URL because the file is located on the server side, not the client side.
With that said, if you are trying to open a file located on the server side and you know the full path to the file; what you need to do is generate the virtual path within your application where the file can be found. You do this by creating a Virtual Directory in your APP from the IIS Admin Manager (Control Panel-->Admin Tools -->IIS Mgmt) and mapping this directory to the actual physical directory.
EDIT:
Say for example your whole website is physically located on the server on c:\inetpub\wwwroot\your_app. Let's assume your app can be accessed via http://example.com and the files you want to serve are physically located on d:\files. Assume further that you created a virtual directory for your app (as I explained above) and that you called this virtual folder public_files. If one knows the file name it should be possible to access the file by simply going to http://example.com/public_files/filename.ext. Since you in your app already know the file name, all you need to pass as parameter to window.open is this url (http://example.com/public_files/filename.txt)
You could try using the AppDomainAppVirtualPath to get the virtual path to the file.
string vPath = HttpRuntime.AppDomainAppVirtualPath + "/my/relative/path"
if the file is not located within the virtual directory there are a number of security issues you need to consider and address. in general it's not a good idea to access a file outside of the virtual directory.
if you must, then you will need to raise security permissions and grant access to the file on the network.

How to change the server response header in asp.net2.0 with IIS6.0 server

How to modify the server values of response header through code behind using asp.net 2.0 with IIS6.0 server.
I have tried Response.Headers.Set("XYZ","ABC");
But it displays throw integrated pipeline error.
Try this:
HttpResponse response = ...;
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", ""); //(write whatever headers you want like this)
I would use the below in Global.asax.cs to remove the Server header
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
}

Resources