What I am trying to achieve is changing the name of uploaded images to a somewhat unique string.
I have created an image gallery which is populated dynamically by database. The gallery is working fine except, that files stored in the database will be problematic if an image with the same name is uploaded!
Code in the controller for file upload:
string AdvertImage = picture1.FileName;
advert.AdvertImage = AdvertImage;
var image1Path = Path.Combine(Server.MapPath("~/Content/Images"), AdvertImage);
picture1.SaveAs(image1Path);
The code below is what I am working on
string BackImage = DateTime.Now.ToString("yyyyMMddhhmmssfffffff");
caradvert.BackImage = BackImage;
var image3Path = Path.Combine(Server.MapPath("~/Content/CarImages"), BackImage);
picture3.SaveAs(image3Path);
I have managed to create a unique file name thou it is not appending .jpg to the end of the image.
Any help or advice on the subject welcome
Try this:
//save file in folder
if (FileUpload1.PostedFile.ContentType.ToLower().StartsWith
("image") && FileUpload1.HasFile)
{
string savelocation = Server.MapPath("savedImages/");
string fileExtention = System.IO.Path.GetExtension(FileUpload1.FileName);
//creating filename to avoid file name conflicts.
string fileName = Guid.NewGuid().ToString();
//saving file in savedImage folder.
string savePath = savelocation + fileName + fileExtention;
FileUpload1.SaveAs(savePath);
}
Related
Previously I successfully showed Image like this:
<img class="centered" src="~/Content/images/gif.gif" style="max-height:12vw;" />
with no problem, but this image was added to the Solution's images folder.
However, when I upload image to API like and store it like this:
string FileName = valueobject.PictureName;
string path = HttpContext.Current.Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
string imgPath = Path.Combine(path, FileName);
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
File.WriteAllBytes(imgPath, valueobject.PictureImage);
//store UserFile with path to the image
UserFile _userFile = new UserFile();
_userFile.FileOwner = valueobject.PictureOwner;
string relativePath = "~/UserFiles/" + valueobject.PictureOwner + "/";
string imgRelativePath = Path.Combine(relativePath, FileName);
_userFile.FilePath = imgRelativePath;
The image is saved - the folder is created and image is saved correctly in localhost.
However I am unable to resolve the relative path in MVC when I try:
<img height="400" style="width:auto" src="#Model.HomeGalleries[g].GalleryImageStrings[i]" runat="server" />
I get "broken" image icon even that
#Model.HomeGalleries[g].GalleryImageStrings[i]
is equal to
imgRelativePath
I am not sure why this does not work.
you must include Server.MapPath for this actual image path. folloing below this line ,like as.
string relativePath = Server.MapPath("~/UserFiles/" + valueobject.PictureOwner + "/");
please try this.hopefully this you help.
i am develop a project that can save and delete uploaded files from local system. here is the sample code and i am using fileupload asp-control in asp.net c#
List<AttachmentModel> attachmentList = new List<AttachmentModel>();
if (fuAttachment.HasFiles)
{
foreach (HttpPostedFile uploadedFile in fuAttachment.PostedFiles)
{
attachment.AttachmentType = "Attachment";
attachment.FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
uploadedFile.SaveAs(Path.Combine(Server.MapPath(#"~/Attachments/" + uploadedFile)));
attachmentList.Add(attachment);
}
}
objEL.AttachmentList = attachmentList;
i am trying to upload multiple files( >=2 files). but i am able to read only one file that is selected first..
so then i have changed one line
attachment.FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
to
attachment.FileName = uploadedFile.FileName;
but with this i am getting the whole path of the file as
"C:\Users\ThirupathiReddy\Downloads\PSK_logo.jpg"
i just want to read the file name ...
thank you..
Try:
var fullFilename = #"C:\Users\ThirupathiReddy\Downloads\PSK_logo.jpg";
var fileInfo = new FileInfo(fullFilename );
var filename = fileInfo.Name;
I have a form which uploads an image to the server, and I am abl to get the filename of the image and other data inside the form.
How would I convert the image to string to be included in a html email?
This is the code which gives me my filename.
string PostedFileName = "not_set";
foreach (string fileName in HttpContext.Current.Request.Files)
{
HttpPostedFile file = HttpContext.Current.Request.Files[fileName];
PostedFileName = fileName;
}
MessageSent("TRUE - " + PostedFileName);
The end-user supplies a path, indicating where the original document is.
string DocxFileName = "C:\\WorksArshad\\3.docx";
I'd like to create a copy of the document name 3.docx as 3Version1.docx and store the copy in the same directory as the original.
How do I get the whole path without the file name and extension?
(i.e.) I need to get the "C:\\WorksArshad\\" path alone.
FileInfo file = new FileInfo(Session.FileName);
string path = file.Directory.tostring();
and then using
string fileName = Path.GetFileNameWithoutExtension(Session.FileName);
string DocxFileNamee = path + "\\" + fileName + "V1.docx";
File.Copy(Session.FileName, DocxFileNamee, true);
where in Session.FileName = "C:\WorksArshad\3.docx" and in path I'd get "C:\WorksArshad"
requirement solved .
Or
File.Copy(Session.FileName, Path.Combine(Path.GetDirectoryName(Session.FileName)
, Path.GetFileNameWithoutExtension(Session.FileName)+"V1.docx"),true);
both the above gives the solution
I have save my Image using Below code In a Folder in my source file..
now I have to retrvive the same saved Image on my Form
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string strtemp = System.DateTime.Now.ToString("ddMMyyhhmmss_") + filename;
fileupload1.SaveAs(Server.MapPath("Image/" + strtemp));
So how should i give a Path to my Image Control
I have tried something like this to get a path of file and folder but simply i cant fetch the image from it into my image folder
Image2.ImageUrl = (Server.MapPath("Image/" + strtemp));
Use it like this:
Image2.ImageUrl = "~/Image/" + strtemp;
Server.MapPath is used to get the physical path of a resource of your server. You need it for operations like saving files. However your physical path is not valid on the web. You should use the virtual path to specify the url.
You can fetch Image like this.:-
Image2.ImageURL = "~/Image/"+strtemp;
Hope this helps you.
Image2.ImageUrl = "~\\Image\\" + strtemp;
Use the above code.
if(!(DropDownList1.SelectedItem.Text==""))
{
string a = DropDownList1.SelectedItem.Text;
string [] q = a.Split('/');
string qq = q[1];
Image1.Visible = true;
Image1.ImageUrl = "~/Images1/" + qq;
}