Display image with " " character in url - c#

I have a problem in my MVC 3 Application
i can't access to my image with url like that:
http://virtualDirectoryImage.fr/ImageNameContains+.jpg
or
http://virtualDirectoryImage.fr/ImageNameContains%2b.jpg
I got Error 404 file not found (the file exist, i can access to others images witch dont contains plus (+) symbol).
How can i do plz?
IIS7 issue? Application issue?
PS: the images are not hosted in my application. (Virtual Directory, on the same Server)
thx for your help

Can you follow the steps given in the following url
http://helpx.adobe.com/cq/kb/CannotOpenAFileHavingSpecialCharactersInTheFilenameOnIIS.html
basically IIS will block the file names if it contains any special characters.Esspecially if it contains & in the file name for security reasons.It's a good practice to replace the special characters with "_" while uploading the file to the server

Related

Visual Studio 2022 C# ASP.NET Webforms with service reference text files not found on server (HTTP Error 404.0)

I am currently trying to create a web service application using Visual Studio 2022 ASP.NET Webforms application with a service reference. The goal is to take in information and store it as a text file on the local machine within the project folder so it is accessible by the web service on my local server.
I have successfully created the text files and can access them on my local machine, but when I navigate to the text file on my local server tree I get an HTTP Error 404.0 which is shown below. I need any user who accesses my server to be able to access the saved text files. I have tried to change security privileges on the folder and in my web.config file, but have not had any luck. I would appreciate any suggestions someone may have.
Here is my code for where I save the information as a text file.
// Randomly generate string for text file name
var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
var textFile = new char[4];
var random = new Random();
for (int i = 0; i < textFile.Length; i++)
{
textFile[i] = chars[random.Next(chars.Length)];
}
eventFile = "\\";
eventFile += new String(textFile);
eventFile += ".txt";
folderPath = Server.MapPath("~/Events");
File.WriteAllText(folderPath + eventFile, fullEventDetails);
Both my URL and local file path are the following:
URL https://localhost:44399/sx1l.txt
Path Name \\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt
Ok, so you have to keep in mind how file mapping works with IIS.
Your code behind:
that is plane jane .net code. For the most part, any code, any file operations using full qualified windows path names. It like writing desktop software. For the most part, that means code behind can grab/use/look at any file on your computer.
However, in practice when you use a full blown web server running ISS (which you not really doing during development with VS and IIS express)? Often, for reasons of security, then ONLY files in the wwwroot folder is given permissions to the web server.
However, you working on your development computer - you are in a effect a super user, and you (and more important) your code thus as a result can read/write and grab and use ANY file on your computer.
So, keep above VERY clear in your mind:
Code behind = plane jane windows file operations.
Then we have requests from the web side of things (from a web page, or a URL you type into the web browser.
In that case, files are ONLY EVER mapped to the root of your project, and then sub folders.
So, you could up-load a file, and then with code behind save the file to ANY location on your computer.
However, web based file (urls) are ONLY ever mapped though the web site.
So, in effect, you have to consider your VS web project the root folder. And if you published to a real web server, that would be the case.
So, if you have the project folder, you can add a sub folder to that project.
Say, we add a folder called UpLoadFiles. (and make sure you use VS to add that folder). So we right click on the project and choose add->
So, you right click on the base project and add, like this:
So, that will simple create a sub folder in your project, you see it like this:
So, the folder MUST be in the root, or at the very least start in the root or base folder your project is.
So, for above, then with UpLoadFiles, then any WEB based path name (url) will be this:
https://localhost:44399/UpLoadFiles/sx1l.txt
(assuming we put the file in folder UpLoadFiles).
But, if you want to write code to touch/use/read/save and work with that file?
You need to translate the above url into that plane jane windows path name. (for ANY code behind).
So, if I want to in code read that file name?
Then I would use Server.MapPath() to translate this url.
say somthing like this:
string strFileName = "sx1l.txt";
string strFolderName = "UpLoadFiles"
string strInternaleFileName = server.MapPath(#"~/" + strFolderNme + #"/" + sx1l.txt";
// ok, so now we have the plane jane windows file name. It will resolve to something like say this:
C:\Users\AlbertKallal\source\repos\MyCalendar\UpLoadFiles\sx1l.txt
I mean I don't really care, but that web server code could be running on some server and that path name could be even more ugly then above - but me the developer don't care.
but, from a web browser and web server point of view (URL), then above would look like this:
https://localhost:44392/UpLoadFiles/sx1l.txt
And in markup, I could drop in say a hyper link such as:
UpLoadFiles/sx1l.txt
So, keep CRYSTAL clear in your mind with working with path names.
Web based URL, or markup = relative path name, ONLY root or sub folders allowed
code behind: ALWAYS will use a plane jane full windows standard file and path.
But, what about the case where you have big huge network attached storage computer - say will a boatload of PDF safety documents, or a catalog of part pictures?
Well, then you can adopt and use what we call a "virtual" folder. They are pain to setup in IIS express, but REALLY easy to setup if you using IIS to setup and run the final server where you going to publish the site to.
Suffice to say, a virtual folder allows you to map a EXTERNAL folder into the root path name of your side.
So, you might have say a big server with a large number of PDF docuemnts,
say on
\\corporate-server1\PDF\Documents
so, in IIS, you can add the above path name, say as a folder called PDF.
Say like this:
So, WHEN you do the above, then the folder will appear like any plane jane folder in the root of the project, but the file paths can and will be on a complete different location OUTSIDE of the wwwroot folder for the web site.
So, now that we have the above all clear?
\\Mac\Home\Desktop\Homework3\Homework3\sx1l.txt
But, your code has this:
folderPath = Server.MapPath("~/Events");
File.WriteAllText(folderPath + eventFile, fullEventDetails);
(you missing the trailing "/" in above, you need this:
File.WriteAllText(folderPath + #"/" + eventFile, fullEventDetails);
So, that means the url for the text file will then be:
https://localhost:44399/Events/sx1l.txt
And if you using Visual Studio to add files to that folder (add->existing items), then MAKE SURE you Build->rebuild all (else the file will not be included in the debug run + launching of IIS express.
So, given that you saving into a folder called Events (as sub folder of wwwroot, or your base folder for hte web site, then the above is the url you should use, but your code always was missing that "/" between folder and file name.

Failed to load resource, error 404, running with IIS Local

Good morning, I developed a mvc 5 application, during development I used IIS Express, all the files there loaded perfectly, when I uploaded to my IIS Local (7) still on my machine to test, I realized that several files are not loaded.
Failed to load resource: the server responded with a status of 404 (Not Found)
glyphicons-halflings-regular.woff
When checking I identified that the file is not loaded because in fact, the path that tries to perform the GET is incorrect. It tries to get get on the path:
http://localhost/content/fonts/glyphicons-halflings-regular.woff
But the correct file would be:
http://localhost/aprovacoes/content/fonts/glyphicons-halflings-regular.woff
In the get, the project name is missing, due to this it is not loaded.
To solve the problem of this particular source, I needed to edit the bootstrap.min.css file and change the url from url:
url(/content/fonts/glyphicons-halflings-regular.woff)
for
url(aprovacoes/content/fonts/glyphicons-halflings-regular.woff)
However in every project several files are not loaded, and to solve I'm editing the css file by file, would not there be another way to map this correct path in all css files?
Thank you very much in advance
Use a resource-relative path. For example, if you have a file structure like:
+ bootstrap.css
- fonts
+ glyphicons-halflings-regular.woff
Then, use the URL: fonts/glyphicons-halflings-regular.woff. Or if it's something like:
- css
+ bootstrap.css
- fonts
+ glyphicons-halflings-regular.woff
Then, you'd use: ../fonts/glyphicons-halflings-regular.woff.
That way, it won't matter how or where the site is deployed, as long as the relative path between the files remains consistent.

Xml file not getting uploaded fully

In my C#/winforms app, using System.Net's HttpWebRequest class, I programmatically upload an xml file to a server(ABC) outside of our company network.
(App user is located in NY and server is located in UK).
We've not been having any issues with this in last 2 years until yesterday when we found out that the file received by the server is not complete making the xml not well-formed and invalid.
I save the xml on our local server before sending it to ABC.
We notice that the size(36 kb) of the this xml is same what was received by ABC but almost half of the xml received by them is empty with lots of blank spaces and some tags appear broken.
Not sure why this could have happened and how to programmatically resolve this going forward.
Would anyone be able to advise on this please?
Thanks for your help in advance.

C# Upload Files on another partition of the server

I'm using FileUpload.SaveAs() function of C# to upload files to the server but I want to save the files on another partition. Let us say, save the files on Drive D of the server instead on the current drive which is Drive C. Please share your thoughts. Thanks is advance.
I have learned that using full path such as
FileUpload.SaveAs("D:\FileUpload");
will save the file outside the web server.
Check this out.
To simplify the question, how can I upload files on the other partition of the server that hosts my web app?
Based on the documentation from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx, the String filename is the full path name of the location to save. Meaning you should be able to do so e.g:
FileUpload.SaveAs("D:\where_you_want_to_save")
By the way what have you tried and what error did you get?
Looking at the example on MSDN, it would appear that .SaveAs() accepts a fully qualified file name as a parameter. You could potentially use a Path object to cleanly build a path for the file, or just specify one directly as a string:
uploader.SaveAs("d:\\someFolder\\someFile.ext");
Resolved this by using Virtual Directory of IIS and providing admin credentials for authentication

Uploading Files with hash(#) in filename to WebDAV Server

I have files with filename containing # in it (eg: #qwerty.txt, file#1.exe).
I want to upload these files to WebDAV Server, running on Linux.
In ASP.Net, I use HttpUtility.UrlEncode() Method for encoding URL.
But, this removes # from the file-names, to be uploaded and thus uploading fails.
Please reply.
Thanks.
UrlEncode does not remove pound/hash characters but replaces them by %23.
Thus #qwerty.txt becomes %23qwerty.txt. If you use that resulting filename to reference the local file for uploading it will fail to find it.
If you want a more definitive answer you might want to update your question with the code you are using to upload the file.

Categories