In C# ASP.Net Website, to transfer the file to client I am using
String file_path = Server.MapPath("~/files/"+file_name);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file_Name);
HttpContext.Current.Response.TransmitFile(file_path);
HttpContext.Current.Response.End();
It is working perfectly, but when the file name contains any spaces the downloaded file has a name only up to the first word. For ex: If my file name is "This is demo.txt" then the downloaded file name becomes "This" with no extension. Hence the user downloading it is not able to identify its type.
How can we avoid happening it for file name containing spaces?
I tried using
String file_path = "'"+Server.MapPath("~/files/"+file_name)+"'";
But it didn't work.
Also its not possible for me to replace ( with '_' or '-') or remove all the spaces present in the file name which are present on the server.
You should enclose the filename in quotes.
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file_Name + "\"");
And if you are using VS2015 or later, you can make it a bit tidier with string interpolation:
HttpContext.Current.Response.AddHeader("Content-Disposition", $"attachment;filename=\"{file_Name}\"");
Also, the filename in the header does not have to be the same as the name of the file. The filename in the header is only a suggestion to the user.
For full details, see RFC 6266 "Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)," especially with regard to which characters may cause problems in a filename.
Related
I am generating a PDF and saving to the hard drive. I have given a default file path. The PDF I save needs to be saved in the following format : BSc/IT/C/ . The problem I have is, when I try to save the PDF to the hard drive, the name of the PDF file is considered as part of the file path as I'm using '/' symbol. But I need to save the file in that exact format.
Here's my code :
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Filepath + "//" + id + ".pdf", FileMode.Create));
In the code, Filepath contains "C:\Users\acer\Documents\Visual Studio 2010\Projects\MyProject" which I have defined in the "web.config" file. The id contains "BSc/IT/C/". Is there any way in which I can save the PDF file in the format I need it to be?
Please help. Thanks in advance.
You can't create file name containing /
Because
The following reserved characters:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
Refer below link for more information naming conventions
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
I have a zip file, which contains one CSV file.
I need to Base64 encode this zip file to send to eBay (using their API).
I used this website: http://www.opinionatedgeek.com/DotNet/Tools/Base64Encode/ which works nicely, I upload my zip file and it returns a base64 encoded string which eBay likes.
I need to do what this website does, but using Classic ASP and VB Script.
I already have a base64 encode function, from here: http://www.motobit.com/tips/detpg_base64encode/ so I don't need a script for that. This function takes a parameter, so I need to turn my zip file into a string (I think) to pass into this function.
I have tried using ADODB.Stream and the LoadFromFile method, but the string it returns, after base64 encoding, doesn't match that from the opinionated geek website and isn't accepted by eBay.
This is what I've tried:
Dim objStream, strFileText
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile Server.MapPath("myzipfile.zip")
strFileText = Base64Encode(objStream.Read)
Response.Write strFileText
objStream.Close
Set objStream = Nothing
Can anyone help..?
Thanks!
This is now solved...
I was missing the BinaryToString function between the stream output and the base64 encode.
Now I use:
strFileText = Base64Encode(BinaryToString(objStream.Read))
Where the new function is...
Function BinaryToString(Binary)
Dim I, S
For I = 1 To LenB(Binary)
S = S & Chr(AscB(MidB(Binary, I, 1)))
Next
BinaryToString = S
End Function
The output from this now matches the output from the opinionated geek tool.
Thanks to ulluoink for pointing me in the right direction!
My code to create a CSV file that contains Danish characters
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/csv;charset=utf-8";
Response.Charset = "utf-8";
Response.Write(sb.ToString());
Response.End();
Don't know why but when result CSV is created danish characters are replaced with some special characters like ??j.Can any one give me any clue?
Is the source text in the StringBuilder UTF-8? If not there is a chance that .NET gets confused and writes the wrong characters. If it isn't in UTF-8 try running Encoding.Convert before writing to the client.
Using ASP's file system object (FSO), I'm trying to read a txt file with OpenTextFile that contains French characters (e and a with accents for e.g). Those characters come out wrong.
I tried specifying the format to TristateTrue to open the file as Unicode but to no avail.
I've been reading about using the ADO Stream object instead but I hoped there would be a way with FSO. Does anyone have any ideas?
Most likely the file is saved in UTF-8 encoding. The FileSystemObject does not handle UTF-8.
Either have the file saved as Unicode or use the ADODB.Stream object. The ADODB.Stream has a LoadFromFile method and does support UTF-8.
Dim s
Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.CharSet = "UTF-8"
stream.LoadFromFile Server.MapPath("yourfile.txt")
s = stream.ReadAll
stream.Close
string filename = "Category#FileName";
response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "text/csv";
response.AddHeader("Pragma", "public");
Final file name => Category_FileName
I would appreciate your help here, pls help!
A filename can't contain the character / under Windows (or most other OSes), so you won't be able to stop that one getting converted. # is left alone by most browsers, the odd one out being IE.
Content-Disposition filenames are a bit limited. Non-ASCII characters aren't reliable in them and the proper escaping according to the RFC doesn't work. If you want to get Unicode characters into a default download filename—or certain other punctuation characters including # in IE—you have to URL-encode it and include that as a trailing part of the path, omitting the Content-Disposition filename. eg:
http://www.example.com/myscript.aspx/foo%23bar.xls
myscript.aspx:
response.AddHeader("Content-Disposition", "attachment");
(You still can't include / in a filename this way as web servers tend to block all URLs with %2F in. In any case, as above, you wouldn't be able to save it with a / in the filename anyway.)
Sadly this is a limitation of IE.
According to Microsoft, the following characters will be converted to underscores if you use them in the file attachment's filename attribute:
< Left angle bracket
> Right angle bracket
\ Backslash
" Quotation mark
/ Slash mark
: Colon
| Vertical bar
? Question mark
* Asterisk
Space
See https://support.microsoft.com/en-us/help/949197/certain-characters-in-a-file-name-may-be-converted-to-underscores-when-a-user-downloads-a-file-by-using-windows-internet-explorer-7