File named rename problem. Strange characters.

S

shapper

Hello,

I have a table where I save the info from files I upload to the
server.
All files are renamed to {Guid}.extension.

When the user downloads the file I rename it using the file title
saved on the database:

var file = (from f in database.Files where f.FileId == id select
file).SingleOrDefault();
return new Microsoft.Web.Mvc.BinaryStreamResult
(System.IO.File.OpenRead(Server.MapPath("~/" + file.Path)))
{ FileDownloadName = file.Title };

The problem is when title has characters like ã, õ, á these are
replaced ... and spaces also become +.

For example:
"São à aões.pdf"

Becomes:
"S%c3%a3o+%c3%a0+a%c3%b5es.pdf"

How can I solve this?

Thanks,
Miguel
 
S

shapper

In URLs, unprintable characters are changed to % and two hex digits.  The
most familiar of these is the space, which becomes %20.

So is there a way around this?
 
S

shapper

Sure.  Just treat the URL as a byte stream, replacing the "unprintable" 
characters with the byte equivalent, then decode the stream of bytes as  
the appropriate character encoding string (probably UTF-8, but I think  
this depends on the encoding you're using to generate the filenames in the  
first place).

Pete

Sorry Pete, I am completely lost ...

Basically, the file is being saved by the user as "S%c3%a3o+%c3%a0+a
%c3%b5es.pdf" instead as "São à aões.pdf".

Yes, I am using UTF-8 on my pages.
 
S

shapper

Yes, we know.  Reiterating your original question doesn't help us  
understand what it was about the answers you've received that you didn't  
understand.

If you didn't understand Michael's description of the encoding used in the  
filename ("% and two hex digits"), you should ask for clarification on  
that.

If you do understand his description, then I don't understand why you are 
completely lost.  Surely understanding his answer means you're at leasta  
_little_ not lost.  :)  But more specifically, his description of the 
encoding, along with my suggestion as to how to decode the encoding,  
should be sufficient.  If you're completely lost, you should be more  
specific.

I don't know what part of the encoding is converting spaces to the '+'  
(plus) character, but if it's important to you, you will need to at least 
convert those back to spaces, or else research your problem better so that  
you understand at what stage the filename is being manipulated and why.  
Unfortunately, since you haven't posted a concise-but-complete code  
sample, no one else can do that research for you.

Pete

Ai ... :) ... I posted the code I have:

public ActionResult Download(Guid id)
{
var file = (from f in database.Files where f.FileId == id select
file).SingleOrDefault();
return new Microsoft.Web.Mvc.BinaryStreamResult
(System.IO.File.OpenRead(Server.MapPath("~/" + file.Path)))
{ FileDownloadName = file.Title };
}

This is an ASP.NET MVC controller action. It receives an id.
Then it looks for the record on the database and reads the file using
file.Path. File path is something like:
Contents\\Documents\\a86ad7a7-04dc-4418-894f-c81b4f943897.pdf

Then I return the stream but renaming the file using file.Title.

I understand the conversion being made to the name with the %20 and so
on ...

What I am completely lost is how to solve that ... so the user has a
"normal" file name when saving the file.

Thank You,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Replace characters. 5
File Name invalid Characters 9

Top