Mapping Uploaded Files to different directory - c#

Using AjaxFileUpload
string path = Server.MapPath("~/Files/") + e.FileName;
This code is uploading files to Files directory under Website folder in asp.net...
How i can map uploaded files to different directory ?
e.g.
Combo Box -> have two option
Image
Doc .
If a user select Image then files uploaded should move to Image folder ..similarly for Doc..
How to write code for this in asp.net c# ?

You just need a conditional to check which option they have selected then place them accordingly. The following code assumes the directories exist, if that's not the case then you'll have to add some logic to create them in the event that they don't.
string path = System.String.Empty;
if (image == true)
path = Server.MapPath("~/Files/Images") + e.FileName;
else
path = Server.MapPath("~/Files/Docs") + e.FileName;
More likely you'll have to do some logic to group them based the file extension. Another option would be to put a radio button for image, in on click listener where the user submits the image you can check whether or not that option is set (my code sample is expecting something like this).

Related

How can I "ping" a text file in a custom Unity3D Inspector?

I'm trying to "ping" a Textfile in a custom editor in Unity3D like e.g. using EditorGUIUtility.PingObject (Shows the file in the Hierachy and flashes a yellow selection field over it).
The file is under Assets/StreamingAssets/Example.csv
The simplest solution (I thought) would be to simply show it in an ObjectField -> if clicking on the field the asset also gets "pinged".
so I'm trying:
// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
TextAsset file = (TextAsset)AssetDatabase.LoadAssetAtPath(path, typeof(TextAsset));
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(TextAsset), false);
But though the file is there and the path correct, file is allways null
Unfortunately, your code is fine and this is a bug.
The bug occurs when the asset is placed in the StreamingAssets folder that is in the Assets folder. This causes the AssetDatabase.LoadAssetAtPath function to fail. I did search about this and only one post came up without any workaround. I mean you can use one of the API from the System.IO namespace to read the file but then you won't have access to the Object that references the file.
Possible Fix:
1.Restart Unity.
2.Create a folder named "Test" in the Assets folder.
3.Drag the StreamingAssets folder to this "Test" folder.
4.Change the path in the code to var path = "Assets/Test/StreamingAssets/" + "Example" + ".csv"; then click "Play". It should not be null. If it's no longer null, move the StreamingAssets folder back to the Assets folder and also change the path in code to the old path.
The steps above is how I fixed it on my side and it works now. If that doesn't work, I suggest you move the "Example.csv" file to the Assets folder then use var path = "Assets/" + "Example" + ".csv"; to read it. If it works, move it back to the StreamingAssets folder and change the path to the old path.
Another thing I suggest you do is call AssetDatabase.Refresh() to refresh the project.
I also suggest you file for bug report for this issue.
My mistake was the typecasting to TextAsset and using LoadAssetAtPath which requires a Type parameter.
Leaving it "uncasted" as object (asset) and using LoadMainAssetAtPath instead which doesn't require the Type parameter works now:
// For debug, later the filename will be dynamic
var path = "Assets/StreamingAssets/" + "Example" + ".csv";
var file = AssetDatabase.LoadMainAssetAtPath(path);
EditorGUILayout.PrefixLabel("CSV File", EditorStyles.boldLabel);
EditorGUILayout.ObjectField(file, typeof(object), false);

Embedding Text File in Resources C#

I'm attempting to do two things. I want to embed a text file into my project so that I can utilise it and modify it, but at the same time I don't want to have to package it when I send the project out to users (I.E included in the exe file).
I've had a look around and there's been multiple questions already but I just cant seem to get any to work. Here's the steps I've taken so far;
Added the text file to my "Resources Folder"
Build action to "Content" and output directory to "Do not copy"
I then try to access the file in my code;
if (File.Exists(Properties.Resources.company_map_template))
{
MessageBox.Show("Test");
var objReader = new StreamReader(Properties.Resources.company_map_template);
string line = "";
line = objReader.ReadToEnd();
objReader.Close();
line = line.Replace("[latlong]", latitude + ", " + longitude);
mapWebBrowser.NavigateToString(line);
}
The MessageBox never appears which to me means that it cannot find the file and somewhere somehow I've done something wrong. How can I add the file into my project so I don't need to distribute with an exe whilst being able to access it in code?
I would use the following:
BuildAction to None (not needed)
and add your file to Resources.resx under files (using DragAndDrop from SolutionExplorer to opened Resources.resx)
Access to your Text:
using YOURNAMESPACE.Configuration.Properties;
string fileContent = Resources.company_map_template;
Then you're done. You don't need to access through StreamReader

Detect when OpenFileDialog returns a downloaded URL/URI

I'm using OpenFileDialog (.Net Framework 4, Windows 10) and I've noticed that it will allow the user to specify a URL as the file name (e.g., http://somewebsite/picture.jpg). This is very useful for my application, so I don't intend to disable it. The way it works is downloading the file into the user's temp directory and returning the temporary file name in the dialog's Filename property. This is nice, except for the fact that the user starts to build up garbage in his/her temp directory.
I would like to tell when a file was downloaded by the OpenFileDialog class (as opposed to a previously existing file), so I can clean up by deleting the file after use. I could check if the file's directory is the temp directory, but that's not very good since the user might have downloaded the file him/herself.
I've tried intercepting the FileOK event and inspect the Filename property to see if it is an HTTP/FTP URI, but despite what the documentation says ("Occurs when the user selects a file name by either clicking the Open button of the OpenFileDialog") it is fired after the file is downloaded, so I don't get access to the URL: the Filename property already has the temporary file name.
EDIT: This is an example of what I'like to do:
Dim dlgOpenFile As New System.Windows.Forms.OpenFileDialog
If dlgOpenFile.ShowDialog(Me) <> Windows.Forms.DialogResult.OK Then Return
''//do some stuff with dlgOpenFile.Filename
If dlgOpenFile.WasAWebResource Then
Dim finfo = New IO.FileInfo(dlgOpenFile.Filename)
finfo.Delete()
End If
In this example, I've imagined a property to dlgOpenFile "WasAWebResource" that would tell me if the file was downloaded or originally local. If it's the first case, I'll delete it.
There's no obvious way to do this, but as a workaround, how about checking where the file lives? It looks like by default this dialog downloads files to the users Temporary Internet Files directory, so you could introduce some code that looks something like this:
FileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string temporaryInternetFilesDir = Environment.GetFolderPath(System.Environment.SpecialFolder.InternetCache);
if (!string.IsNullOrEmpty(temporaryInternetFilesDir) &&
dialog.FileName.StartsWith(temporaryInternetFilesDir, StringComparison.InvariantCultureIgnoreCase))
{
// the file is in the Temporary Internet Files directory, very good chance it has been downloaded...
}
}

Using an image in the resource folder in a function which takes file path to image as parameter

I'm developing a winform application. It has a reference to a dll library. I want to use a function 'PDFImage' in this library. This function is used to put images into a PDF documnent. The function 'PDFimage' has an argument 'FileName' of type String which takes the file location of the image.
Now I have to put the image as a separate file with the .exe file created after the project is built. This is not convenient for me. What I do now is I mention the file name of the image as the function parameter like 'Flower.jpg'. And I have kept the image in the \bin\debug folder.
I don't want to do it like this as this needs the image file to be placed seperately with the executable file.
What I am trying to do is as follows:
I added the image files to the Resources folder as existing item. Now, to call the function PDFImage, I need to pass the file name as argument. How can I do this?
I have the source code of dll with me. Is it better to modify the source code as required and create another dll rather than what I am doing now?
See if this helps;
string apppath = Application.StartupPath;
string resname = #"\Resource.bmp";
string localfile = "";
localfile = apppath + resname;//Create the path to this executable.
//Btw we are going to use the "Save" method of Bitmap class.It
//takes an absolute path as input and saves the file there.
//We accesed the Image resource through Properties.Resources.Settings and called Save method
//of Bitmap class.This saves the resource as a local file in the same folder as this executable.
Properties.Resources.Image.Save(localfile);
MessageBox.Show("The path to the local file is : " + Environment.NewLine + localfile + Environment.NewLine +
"Go and check the folder where this executable is.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
//localfile is the path you need to pass to some function like this;
//SomeClass.Somefunction(localfile);
Hope this helps and here is a sample if you need.
All you can do with that is get the resource, save it to a file (temporary one may be) and then pass the filename to the function. Most function that take a file in .net also take a stream, so if you have control of both sides, I'd do that and then you don't have to mess about with the file system.

Saving the opened file in particular folder

I have a dropdownlist and a textbox. When i write some name and select some options in dropdownlist like word or excel or powerpoint, that particular file opens as an attachment and i should save it in "data" folder which is already present in the solution explorer. My code is like this
string file = TextBox1.Text;
if (dd1.SelectedIndex == 1)
{
Response.ContentType = "application/word";
Response.AddHeader("content-disposition", "attachment;filename =" + file + ".docx");
}
How can I store this file in "data" folder?
The simple answer is "you cannot". Saving file is taking place on the client side. Your web application knows nothing about the client machine (not even whether it's a real browser or another script) and has no control over the client. The end user will have to manually select data folder to save the file to.

Categories