ASP.Net MVC FileStreamResult, valid chars for FileDownloadName - asp.net-mvc

I have an action method that returns a FileStreamResult, the download works fine, the problem is that although I set the FileDownloadName property of the result object, some of the files are downloaded with another name (specifically the last part of the address of the page I'm working on. e.g. in the page "http://localhost:5479/Items/Edit/277" it will download a file called "277").
This happens when the name of the file contains special chars (e.g. "San José.jpg"), but it works just fine when the name doesn't have such chars (e.g. "San Jose.jpg").
So, my question is, how do I allow the user to download a file with special chars in its name? or, if it isn't possible, is there a method that removes all the special chars from a string or do I have to create one?
Thanks

Actually I just found a way to fix this.
Basically what I have to do is use the HttpUtility.UrlEncode method to convert the name of the file, when the file is downloaded it will get almost the same name as the original file (the difference being the spaces replaced with a plus (+) sign).
Hope this helps someone else.

I have the same Problem. A better solution is HttpUtility.UrlPathEncode(...) since it doesn't replace space by '+'.

Related

substitute space character in lua

I am creating a template in Wikipedia to link articles to an external website which is a book archive called fadedpage.com. I am generating a url to link to a specific author page. Part of the url is the author's name which contains one or more spaces. For example, the url for the author "Ian Fleming" is: http://fadedpage.com/csearch.php?author=Fleming, Ian. My template call structure is {{FadedPage|id=Fleming, Ian|name=Ian Fleming|author=yes}}.
For my template I am replicating an existing template which uses a script coded in lua to parse the template arguments. I have been able to generate all of the url except for the space character between the last and first name.
I could code the template call as: {{FadedPage|id=Fleming,%20Ian|name=Ian Fleming|author=yes}} which works OK but I would rather have the call format as it looks on the fadedpage website, ie. with the embedded space. So I need a way in lua to find the space character within the string and substitute it for the string "%20". So far I haven't figured out how to do it. Any help would be appreciated.

How to confirm partial line of text in Ruby

I'm writing a test to confirm that a csv file has hit my downloads folder. As the title of the csv file is set to include the date and time of the download, it's impractical to keep changing the name of the file in my feature. Example filename: fleet_123456_20140707_103015.csv
Can I include in my ruby code, something that will just confirm that the "fleet_123456" is present as it's the only generic part of the name that will appear on every download?
At the moment I have:
Then /^I should get a download with the filename "(.*?)"$/ do |file_name|
page.response_headers['Content-Disposition'].should include("filename=\"#{file_name}\"")
end
I'm thinking that the "#{file_name}\"") needs tweeking, just not sure where.
Any help would be great, thank you
You asked:
Can I include in my ruby code, something that will just confirm that the "fleet_123456" is present as it's the only generic part of the name that will appear on every download?
Yes, you can. One way would be to replace the include matcher with a regex-based one. For example, instead of
page.response_headers['Content-Disposition'].should include("filename=\"#{file_name}\"")
you could write
page.response_headers['Content-Disposition'].should match(/filename="fleet_[\d_]+.csv"/)
, which would match "fleet_123456" followed by any combination of numbers and underscores, followed ".csv". Another possibility, if you want to be a little more specific, is
page.response_headers['Content-Disposition'].should match(/filename="fleet_123456_\d+_\d+.csv"/)
which matches the specific arrangement of groups of numbers separated by underscores. You can read about regular expressions in Ruby here and play with them here.

Best format for adding a version id into a URL path

I'm currently re-working an application and want to add in a version number to the application URL paths. For example:
http://mydomain/app/VERSION-ID/resource/...
My question is, what is the correct or standard format to add a version id to a URL string? Is there any disadvantage to just having it numeric (1.1 or 1-1):
Example: https://api.twitter.com/1.1/account/verify_credentials.json
Or is it better to have a non numeric identifier to be more intuitive as the url is public facing?
Thanks.
Do not use dots in a URL unless you're defining domain spaces. Use either dashes or other truncated versions (that don't use disallowed characters in the URL).
EXAMPLE:
Example: https://api.twitter.com/v1-1/account/verify_credentials.json
UPDATE: Here is some more information in another thread. My preference is not to use dots if at all possible, but it is apparently OK to do.
Can urls contain dots in the path part?

What is the term for the last part of the path of a URL

What term do you give the part of the url after the last slash, but before the query? It seems most places people call it "the last part of the path" e.g. here but that is just so... wordy.
E.g. "something" in this url:
http://www.example.com/path/to/something?param=foo
Update:
I was hoping there was a well-known answer that I was not aware of, but it seems there is still some debate so, that's my answer right there. Guess I'll just keep calling it "the part of the path after the last slash". But I'll leave this question open anway, in case someone makes a convincing argument that gets lots of upvotes.
I'm a bit late to the party, but where I work we call it the slug, as mentioned here for example: https://prettylinks.com/2018/03/url-slugs/
protocol://server.domain/path?query
The path element (and it appears there is no 'defacto' definition) in my mind is the path to the resource on the server. No matter whether the resource is a file (blah.html) or a folder (/path/) it still instructs the server to use the path to find the resource.
Now there appears to be another definition at good/bad ol' Wikipedia here which states that it is usually "http://server/path/program?query_string" where the end resource is defined as 'program' but I think this is incorrect (is a folder a program?)
So.. perhaps its should be
protocol://server.domain/path[/resource.*]?query
? /../../ I traverse...
If URL is like /path/to/file.html or example.com/path/to/something.php?param=foo
then I think we can call it filename as mentioned at http://httpd.apache.org/docs/2.2/mod/directive-dict.html
In my experience it is called a query string whenever it is instructing action on the website. Code is frequently written to address the query string, which would prompt customized results on the page.
Otherwise, I agree it would be called a file name, if the path was referencing a file.
I saw a few uncertain suggestions here and there, including "filename", "basename". The Qt project has these names in their lexicon, so it can serve as a standard.
Namely check out the QFileInfo class.
They have standardized the names as the following:
QFileInfo fi("/tmp/archive.tar.gz");
fi.fileName(); // "archive.tar.gz"
fi.baseName(); // "archive"
fi.completeBaseName(); // "archive.tar"
fi.suffix(); // "gz"
fi.completeSuffix(); // "tar.gz"
This is a specification for local files however, so in this standard, there is a departure from basic URI terminology here:
fi.path(); // "/tmp"
fi.filePath(); // "/tmp/archive.tar.gz"
Where as in QUrl, you have path & fileName
QUrl("file:file.txt").path(); // "file.txt"
QUrl("/home/user/file.txt").path(); // "/home/user/file.txt"
QUrl("http://www.example.com/test/123").path(); // "/test/123"
and
QUrl("file:file.txt").fileName(); // "file.txt"
QUrl("/home/user/file.txt").fileName(); // "file.txt"
QUrl("http://www.example.com/test/123").fileName(); // "123"
If I were building a library, I personally would adopt QFileInfo terminology and apply to both URI and local files, and hope the entire rest of the world has enough sense to follow me.
I found the terms "last segment" or "last chunk" quite accurate and succinct.

ASP.NET MVC Colon in URL

I've seen that IIS has a problem with letting colons into URLs. I also saw the suggestions others offered here.
With the site I'm working on, I want to be able to pass titles of movies, books, etc., into my URL, colon included, like this:
mysite.com/Movie/Bob:The Return
This would be consumed by my MovieController, for example, as a string and used further down the line.
I realize that a colon is not ideal. Does anyone have any other suggestions? As poor as it currently is, I'm doing a find-and-replace from all colons (:) to another character, then a backwards replace when I want to consume it on the Controller end.
I resolved this issue by adding this to my web.config:
<httpRuntime requestPathInvalidCharacters=""/>
This must be within the system.web section.
The default is:
<httpRuntime requestPathInvalidCharacters="<,>,*,%,&,:,\,?"/>
So to only make an exception for the colon it would become
<httpRuntime requestPathInvalidCharacters="<,>,*,%,&,\,?"/>
Read more at: http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestpathinvalidcharacters.aspx
For what I understand the colon character is acceptable as an unencoded character in an URL. I don't know why they added it to the default of the requestPathInvalidCharacters.
Consider URL encoding and decoding your movie titles.
You'd end up with foo.com/bar/Bob%58The%20Return
As an alternative, consider leveraging an HTML helper to remove URL unfriendly characters in URLs (method is URLFriendly()). The SEO benefits between a colon and a placeholder (e.g. a dash) would likely be negligable.
One of the biggest worries with your approach is that the movie name isn't always going to be unique (e.g. "The Italian Job"). Also what about other ilegal characters (e.g. brackets etc).
It might be a good idea to use an id number in the url to locate the movie in your database. You could still include a url friendly copy of movie name in your url, but you wouldn't need to worry about getting back to the original title with all the illegal characters in it.
A good example is the url to this page. You can see that removing the title of the page still works:
ASP.NET MVC Colon in URL
ASP.NET MVC Colon in URL
Colon is a reserved and invalid character in an URI according to the RFC 3986. So don't do something that violates the specification. You need to either URL encode it or use another character. And here's a nice blog post you might take a look at.
The simplest way is to use System.Web.HttpUtility.UrlEncode() when building the url
and System.Web.HttpUtility.UrlDecode when interpreting the results coming back. You would also have problems with the space character if you don't encode the value first.

Resources