Upload image in folder and rename it - c#

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

Related

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

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)

Mapping Uploaded Files to different directory

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

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.

Accessing image from other server

I have image file placed on the one server and application on other server.
I want to access that image, below code I have written:
On default.aspx, I have
<asp:Image ID="Image1" runat="server" ImageUrl= "GetImage.aspx?imgName=MyImage.jpg" />
and on GetImage.aspx, I have written the below code on page_load
protected void Page_Load(object sender, EventArgs e)
{
// Changing the page's content type to indicate the page is returning an image
Response.ContentType = "image/jpg";
var imageName = Request.QueryString["imgName"];
var path = "//SERVER/FOLDER/" + imageName;
if ((string.IsNullOrEmpty(imageName) == false))
{
// Retrieving the image
System.Drawing.Image fullSizeImg;
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
// Writing the image directly to the output stream
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg);
// Cleaning up the image
fullSizeImg.Dispose();
}
}
But I am getting error at
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
Please let me know where I am incorrect. Do I need to anything else other than Server.MapPath ? because my image is on other server.
EDIT
I have image folder in my computer
I have created a web app in other computer [same network], deployed
in IIS, image is displayed correctly. With path like
http://10.67.XX.XX/websiteName/Default.aspx
but when I am trying to access the same from my comupter or any other
computer, I am not able to see the image.
You shouldn't use Server.MapPath. This is used to map virtual paths under your site to physical paths under file system. If the file exists on another server, just access it by name directly, without Server.MapPath.
The answer above is incorrect because the images reside on a separate server.
So System.Images will not no where the image is.
Secondly you have to use server.Mappath for system images, it requires a windows path i.e. c:\blah\blah\whatever.jpg.
you will need to use \\server\C$\folder\image.jpg this works well with system.image.FromFile
Also saving you can also utilize this.
Cheers

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