I use following code to open a file. When I was prompt to open/save the file, it showed the file was from localhost, but the file could be in a network drive. Is there any way I chould replace the localhost with its actual location?
byte[] bts = System.IO.File.ReadAllBytes(fileName);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "Application/octect-stream");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + ((LinkButton)sender).Text);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
Edit: ((LinkButton)sender).Text) is the filename without path. The actual location of the file is, for example, Y:\SPR_Upload, and Y is a network drive that is mapped to C$(\m6300-7). When I clicked the file, I was prompted Do you want to open or save XXX(24.1KB) from localhost? I want to replace the localhost with m6300-7. How do I do that?
The issue appears to be that you’re using
to get the filename. If the LinkButton that’s calling this has
localhosthard-coded into its Text value, you’ll have to correct it there.If you’ve hard-coded the Text property of your LinkButton to something like
http://localhost:0000/...., you’ll need to correct that. With near-zero exceptions, all links in your application should be relative, such as/home/indexor/support/default.aspx.Note that the
Content-Dispositionheader just sets the filename that the browser uses or presents to the user – it can be set to anything, and doesn’t have to reference a real file on the server.As such, if you just want the filename (stripping all path information, etc), you can use
then use the resulting value in your
Content-Dispositionheader assignment.Update
When the browser says “Would you like to download xxxx.xx from localhost?”, it gets the server name (localhost in this case) from the URL that is used to download the file. If you run the same app on, say, “www.mysite.com”, the browser will say “Would you like to download xxxx.xx from http://www.mysite.com?” For security reasons, etc, you shouldn’t be able to override the name of the server used in that prompt.
The
Content-Dispositionheader just sets the filename that the browser shows the user.