How to manage my directory's in asp.net using fileupload - c#

Please take some of your time and read my problem
I need by using fileupload to upload just images to my web application
But when I select the image and click on upload button if the program see it a jpg file it will create a directory with the name jpg an save it there
That goes to gif and png
Please any one if you could give the code I will be very very thankful to you
Or at least link could help me
string folder = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName);
string path = Request.PhysicalApplicationPath + "/" + folder;
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
string server_path = Request.PhysicalApplicationPath + "/myfiles/";
FileUpload1.SaveAs(server_path + FileUpload1.FileName);

You can use System.IO.Path.GetExtension(FileUpload1.FileName); to get the extension of the file.

Physical path for the virtual directory using:
System.Web.Hosting.HostingEnvironment.MapPath(virtualDirectoryPath)

Related

c# Saving richTextBox Data to txt file

I have a rich text box in c# with data in it and I need it saved in a txt document on the desktop.
I have tried this:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
using(File.Create(path));
txtLogBox.SaveFile(path, RichTextBoxStreamType.RichText);
AI get an error that I can't save to the desktop. ANy help would be great.
You're trying to create a file that has the name of an existing directory. You need to append a path separator (Path.DirectorySeparatorChar) and a file name.
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
txtLogBox.SaveFile(path + Path.DirectorySeparatorChar + "example.txt", RichTextBoxStreamType.RichText);
(Your using statement is unnecessary here because RichTextBox.SaveFile(string) handles this for you.)

Upload image in folder and rename it

I have created a interface where user will select and upload image. But how can i rename and save image in server folder(images), and display it again on my image gallery page. My website is developed in asp.net c#.Suggest me some ideas or links to refer.
upload button click code below:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
Thanks.
You will need, obviously, a folder somewhere in the website's directory. The user who is running the application pool for the web application will need read/write access to it. Depending on your needs, you may not want this folder to be browsable directly by users (other than the user running the application pool).
You will need to store the new file path somewhere (database, XML file, flat text, etc.) so when the user requests to see the image, you will open this file, and return it in the response stream as its proper type (JPG, GIF, BMP, etc.) so the user is prompted to open it or save it.
For Rename and save image in server folder try below code : -
//Get Filename from fileupload control and rename it
string filename = ((DateTime.Now.ToString().Replace("/", "-")).Replace(":", "")).Replace(" ", "_") + "_" + ((Path.GetFileName(FileUpload1.PostedFile.FileName)).Replace(".",".")).Replace(" ","");
FileUpload1.SaveAs(Server.MapPath("~/uploads/" + filename));

Distributing and there is some problems

I will be distributing my program. It will take pictures and save them to a folder. The problem is: C:/Users/G73/Desktop/
Everyone has there own file path... In the code it is
bitmap.Save("C:/Users/G73/Desktop/My OVMK Photos//OpenVMK" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg", ImageFormat.Jpeg);
It has my File path and the name of my computer... How would I make it to change to the users path?
Try this code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(path, "My OVMK Photos//OpenVMK", DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"), ImageFormat.Jpeg);
It gets Desktop path for current user. You can get more special folders using Enviroment.SpecialFolder
To get the user's desktop -
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Which you would use with a Path.Combine - e.g.:
bitmap.Save (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "My OVMK Photos//OpenVMK...
Though for images you'd likely be better off using the My Pictures directory -
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Try this
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

Asp.net upload image doesn't work correctly

I am currenlty stuck on a small project. Here is my code;
String StuPicc = Server.MapPath("images/" + UploadPic.FileName);
UploadPic.SaveAs(StuPicc);
UploadPic is the control name. But, neither it display the name nor upload the selected image.
When i output the result on a label it display;
images/{image name isn't showing here}
Any help regard to this will be apreciatable.
If you are using a file upload control then..FileUploadControl ID="FuImage" file upload control..and uploaded files will be saved in "UploadedFiles" folder.
string path = "\\UploadedFiles\\" + Guid.NewGuid() + FuImage.FileName;
FuImage.SaveAs(Server.MapPath(".") + path);
Hope this helps...
Are you using the FileUpload control? I believe the filename is available from the inputfile property then, although the only code I have used is to retrieve the stream:
System.Drawing.Image i =
System.Drawing.Image.FromStream(flUploader.PostedFile.InputStream);
flUploader.PostedFile.Filename should be valid along with the input stream if the upload is occuring.

Saving images on the server

I'm getting the "generic error occurred on GDI++ wen trying to save images.
What I am missing here?
string appdatapath = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
if (!Directory.Exists(appdatapath + "/Images")
Directory.CreateDirectory(appdatapath + "/Images", Directory.GetAccessControl(appdatapath));
if (!Directory.Exists(appdatapath + "/Images/XBLContent/"))
Directory.CreateDirectory(appdatapath + "/Images/XBLContent/", Directory.GetAccessControl(appdatapath));
string imagesdir = Path.Combine(appdatapath, "/Images/XBLContent/");
Image image = FeedUtils.RequestImage(String.Format({0}/{1}/image.jpg", url, c.GUID));
image.Save(imagesdir + c.GUID + "sm.jpg");
Check whether your IIS worker process has write permissions to the local file system, and in particular the folder whether you are saving the image.
Also, use Server.MapPath() to get the physical location.
See this blog for a solution.

Categories