Server.MapPath and window.open() - c#

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.

Related

Image folder creation in web api using concatenate strings

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);

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 + "\"");‌

Run application | app say error

I try to run my game in my game launcher but it says:
Memory access violation
Code
String name = GetValue(File.ReadAllLines(Co), "lib-name" + DubDown);
System.Diagnostics.Process.Start(path + "\\Games\\" + name + "\\" + GetValue(File.ReadAllLines(Co), "lib-file" + DubDown));
String name = GetValue(File.ReadAllLines(Co), "lib-name" + DubDown);
Why are you reading all lines of a file, but the file name shown below is used?
Does the file contain the names of all the files you want to open?? I think you'll need a loop to do that
String name....
shown above is included in the code below twice when written here
... + name +....
and here (as the value of name)
....GetValue(File.ReadAllLines(Co), "lib-file" + DubDown));
Try writing this like this.
System.Diagnostics.Process.Start(path + "\\Games\\" + name);
Unfortunately, as noted above, you'll need to iterate through the contents of the file prior to getting the names from inside the file, as your first line of code portrays. Sort of.
I think you should post more of the code for better assistance.

Writing a CSV log file

I have a Winforms program that needs to log data points into a .CSV file. It's fairly simple, date/time and a double (the data), and go to the next line.
Here's what I have so far (not working, I get an error saying the file is busy/already open - however, it's empty)
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogFileName = SavePath.Text + "\\LOG\\Seeing-Log-" + TimeNow.ToString("yyyy-MM-dd") + ".csv";
if (!File.Exists(LogFileName))
File.Create(LogFileName);
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
It's that last line that generates the error.
Any idea what I am doing wrong?
thanks
Steve
File.Create returns an open FileStream to the file that's just been created. Either change your code to work with FileStream in both the non-existent and existent file cases, or just close the file after creating it:
if (!File.Exists(LogFileName))
File.Create(LogFileName).Close();
But, of course, if you check the documentation for AppendAllText:
Appends the specified stringto the file, creating the file if it does not already exist.
You'll realise that the above two lines are completely redundant anyway and can be removed:
if (!Directory.Exists(SavePath.Text + "\\LOG"))
Directory.CreateDirectory(SavePath.Text + "\\LOG");
string LogString = TimeNow.ToString("yyyy-MM-dd-_HH-mm-ss") + "," + FWHM_Value.ToString("F:");
File.AppendAllText(LogFileName, LogString + Environment.NewLine);
You can even use the free looging tools. Here is one 'log4net'
You can also write the csv file using this. I am assuming currently you are not using logging tool. it will work for you without any code for implementation .
http://element533.blogspot.in/2010/05/writing-to-csv-using-log4net.html
Have a great day!!
Replace
File.Create(LogFileName);
with
File.Create(LogFileName).Close();
see this to create empty file.
The file is locked when you create it. Just update your code to this:
File.Create(LogFileName).Close();

Categories

Resources