Image folder creation in web api using concatenate strings - c#

I am new to c# ,Here I'm trying to form a URL to store the image in API itself .
I want to create a folder in the following structure
Images_Folder --> Fruits_Folder--> Seedless_Folder --> Image.jpg
coding :
string imageURL = HttpContext.Current.Server.MapPath("~/Images/");
folderPath = imageURL + formData.RootFolder + formData.TypeFolder + "/";
filePath = (folderPath + postedFile.FileName);
postedFile.SaveAs(filePath);
By using the above code it stores the image in the following structure.
Images-->Fruits_FolderSeedless_Folder --> Image.jpg
While debugging the code I could see the URL format as follows
"D:\Projects\Dot Net\FruitsDisplay\FruitsDisplaySolution\Images\FruitsSeedless/"
Ex: Images-->FruitsSeedless-->Image.jpg
But I want it should be as Images-->Fruits-->Seedless-->Image.jpg
can anyone help me to solve this.

Let OS decide what to use for sub directories, as it might not always be the familiar \ character. Using Path.Combine() method uses the character that is valid in that environment:
folderPath =Path.Combine(imageURL,formData.RootFolder, formData.TypeFolder, postedFile.FileName);

Related

Defining folder path

I have a rather curious problem.
I have this code (written by someone else). It is suppose to create couple of folders withing each other upon clicking on a specific button. It uses this:
string directoryCrate = "/" + comboIndustry.Text + "/" + comboCustomerName.Text + "/"
Now, as you see, the separator for the folders are "/". But later in the code I need to have the full path of the folder which is c:\projects and then the path to the new folder which is /Industry/Customer/...
So if I want to get the address in the clipboard it would be something like this: C"\projects/Industry/customer/... and obviously I cannot open this address in explorer! For some reason the method won't accept "\" when I try. I am quite confused. Can anyone help?
You can use
System.IO.Path.DirectorySeparatorChar
To get the standard directory separation char for your system. Also, if you try to do a replace, make sure to start the string with an # so it doesn't parse the backslashes or else escaping them as:
string backslash = #"\";
string anotherBackslash = "\\";
Try this:
string path = System.IO.Path.Combine(#"C:\Projects", directoryCrate)
string fullPath = System.IO.Path.GetFullPath(path)
or combine it into one statement.
string fullPath = System.IO.Path.GetFullPath(System.IO.Path.Combine(#"C:\Projects", directoryCrate))
Either of the above two should give you the complete path with backslashes.

How to open files using theirs default application in SilverLight Webapplication?

Can anyone please suggest a method to open the the files using their
default application in Silverlight application. I am able to get the
full path of the files that I am selecting.
That is for verifying the files before uploading. While using this:
AutomationFactory.CreateObject("WScript.Shell").Run(FileList[_index].filepath);‌​
I get
System.IO.FileNotFoundException
Its not working if the filename contains whitespaces in it.
If you want to open filepaths with spaces you need to add quotes arround your path. Try to use:
"\"" + FILE_PATH + "\""
In your code:
AutomationFactory.CreateObject("WScript.Shell").Run("\"" + FileList[_index].filepath + "\"");‌

To check whether image file exist or not

string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);
var isFileExist = File.Exists(serverPath);
The value of event.Image_Url = /Resources/images/event-images/e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But it always gives false even though the file exist on the physical path.The physical path which it comes from above operation as shown below.
serverPath value = D:\Freelance Work\Trunck\Api\web\Resources\images\event-images\e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But actually I need to go to the web folder.But it automatically gets the Api folder as shown above.How to avoid it ? Why it takes the Api folder ? Any help would be highly appreciated.
Note : I have noticed that the web api project is also running on local host.May be that is the reason for it.But how can I tell it to get the virtual path from the web project ?
Folder structure within Trunck as follows.All are in same level.
Trunck --> API
--> Web
--> BLL
The problem was the / at the start of the event.Image_Url field. That caused the last path in the string to be taken as a absolute path.
string serverPath = Path.Combine
( HttpContext.Current.Server.MapPath("~")
, #"..\web\"
, #event.Image_Url.TrimStart('/').Replace('/', '\\')
);
I think you need to try string path= Server.MapPath("~/web"), serverPath =Path.Combine(path,event.Image_Url) instead of string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);

Server.MapPath and window.open()

I'm actually working on an app that provides the possibility to the users to upload the files they wish. Those files should also be visualizable once uploaded.
In order to do that I'm trying to get the file path with Server.MapPath and a concatenation of other values. The file path is passed as an argument in a window.open javascript function.
My problem is that I do not get any result at all. No window is opened.
Here is my code:
string completeUrl = Server.MapPath(ConfigurationManager.AppSettings["UsersImagesUploadFolder"] + CurrentUserLogin +
#"\\" + ((GridDataItem) e.Item)["Url"].Text);
string radWindowOpen = "<script type='text/javascript'>window.open('" + completeUrl + "')</" + "script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "fileDisplay", radWindowOpen);
I'm probably missing something obvious but I don't see what it is.
Thank you for your answers.
As Damien has pointed out, Server.MapPath is used for server side path mapping. Clients need to see a path underneath your web app.
For example:
Page.ResolveUrl("~/uploads/" + ConfigurationManager.AppSettings["UsersImagesUploadFolder"] ...
Would resolve a to http://mydomain/vroot/uploads/... etc.
As an aside, note also that #"\\" would result in a double backslash, which I don't think you intended.
Either of #"\" or "\\" would result in a single backslash.

Managing absolute path and full path

I've created a small program wich can read a .txt file.
This file contains a link to another file in this format new_file.txt
The goal is to return the path of the new file, so basically I'm doing this :
String newFileName = getFileName();
int index = oldFilePath.lastIndexOf('\\');
String path = oldFilePath.substring(0, index + 1);
String newFilePath = path + newFileName;
return newFilePath;
For example :
The first file I opened is : C:\a\b\c\oldFile.txt
In this file I found newFile.txt
So the new path will be : C:\a\b\c\newFile.txt
Nice, but what If I find something like this :
..\ or .\.\ or ...
Is there any way to automate this mess ?
Thanks
In C#/.Net you have the rather cool Path class.
You can use Path.GetFullPath( string pathname ) to resolve paths e.g. with \..\ etc in them.
Use Path.GetDirectory(), Path.GetFileName(), Path.GetFileNameWithoutExtension() & Path.GetExtension() to pull names apart and Path.Combine() to put them back together again.
You've tagged this as java as well as c#
In java look at FileNameUtils http://commons.apache.org/io/apidocs/org/apache/commons/io/FilenameUtils.html
The normalize method should help

Categories

Resources