I have a folder where images are uploaded and named 'Photo1', 'Photo2', etc depending on how many photos are uploaded. In another panel on the same aspx page I want to display the uploaded photos. I will know the filenames but the extension could be .png, .jpg, or .jpeg.
How can I set the ImageUrl path when I know only the filename and not the extension?
Here is an example of how I've attempted it so far--
ASPX Page Code:
<asp:Image ID="Image1" runat="server" />
Code Behind:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.*");
This method says I've used an invalid character. Although, I've tested it by going into the folder and finding the extension then entering the line as:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg");
and that didn't work either, so there may be more wrong here than just the unknown character in the extensions place.
string jpg = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg"
string png = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.png"
if (File.Exists(Server.MapPath(jpg)))
{
}
else if(File.Exists(Server.MapPath(png)))
{
}
If you don't have the file's extension, you may need to read the first few bytes of that file to determine if an image is in what format.
This snippet may help: https://gist.github.com/ChuckSavage/dc079e21563ba1402cf6c907d81ac1ca
Related
I want to upload mp3 file using file uploader and I don't want to save it in the database I used server.mappath
if (FileUploadsong.HasFiles)
{
FileUploadsong.PostedFile.SaveAs(Server.MapPath("songs" + "/" + Txtsongname + "~/mp3file/"));
byte[] mp3file = System.IO.File.ReadAllBytes("songs");
}
but I got this error:
The error tells you that you path name is invalid and cannot be found.
That is, because you are using the control itself as part of your path, instead of its .Text property:
if (FileUploadsong.HasFiles)
{
FileUploadsong.PostedFile.SaveAs(Server.MapPath("songs" + "/" + Txtsongname.Text + "/mp3file/"));
byte[] mp3file = System.IO.File.ReadAllBytes("songs");
}
And please, post your errors as text, not as an image. Screenreaders cannot interpret images and if the image does get deleted, so does your error message for everyone.
I am uploading file.so I want to change my file name.
string createfolder = "E:/tmp/jobres/" + uId;
System.IO.Directory.CreateDirectory(createfolder);
string newfilename = txtname.Text + "Resume" + fileExtension;
AsyncFileUpload1.SaveAs(Path.Combine(createfolder,newfilename ));
This time file is in different format then its not replacing.
string createfolder = "E:/tmp/jobres/" + uId;
System.IO.Directory.CreateDirectory(createfolder);
//AsyncFileUpload1.SaveAs(Path.Combine(createfolder,"Give Something Here whatever is changing at all time like time value"+AsyncFileUpload1.PostedFile.FileName));
AsyncFileUpload1.SaveAs(Path.Combine(createfolder,"DateTime.Now"+AsyncFileUpload1.PostedFile.FileName));
I'm trying to download files from a remote location. But right before the download, I get my file locations from a web service, also on a remote location.
The thing is, I get a degrading performance over time. The downloaded file numbers decrease from around 2k in 3 minutes to 300-400 in the same time after an hour or two and I have 250k files.
Is the service or the download a problem? Or both?
I download files as below after I get the names from the service,
try
{
using (WebClient client = new WebClient())
{
if (File.Exists(filePath + "/" + fileName + "." + ext))
{
return "File Exists: " + filePath + "/" + fileName + "." + ext;
}
client.DownloadFile(virtualPath, filePath + "/" + fileName + "." + ext);
return "Downloaded: " + filePath + "/" + fileName + "." + ext;
}
}
catch (Exception e) {
return"Problem Downloading " + fileName + ": " + e.Message;
}
if (File.Exists(filePath + "/" + fileName + "." + ext))
Bottleneck is probably here.
When you get hella tons of files in a single folder checking if file with this name already exists might need some time to complete.
So you might want to store files in different folders
The problem was the information put on the richTextBox and label.
The rtb was appended with info regarding whatever happened to each individual element. The Label showed at which element we were. Apparently the cpu can't handle it and this becomes a major problem when running for prolonged time. It ate so much cpu that it eventually killed the application. Removing or limiting their output solved almost all problems.
On the other hand, the slight degradation (1.5k to 1.2k per minute after 2.5 hours) of the download that still exists is still a mystery.
I have a website admin section which I'm busy working on, which has 4 FileUpload controls for specific purposes. I need to know that , when I use the Server.MapPath() Method Within the FileUpload control's SaveAs() methods, Will it still be usable on the web server after I have uploaded the website? As far as I know, SaveAs() requires an absolute path, that's why I map a path with Server.MapPath()
if (fuLogo.HasFile) //My FileUpload Control : Checking if a file has been allocated to the control
{
int counter = 0; //This counter Is used to ensure that no files are overwritten.
string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]); // This is the part Im wondering about. Will this still function the way it should on the webserver after upload?
if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
{
while (System.IO.File.Exists(logo))
{
counter++; //Here the counter is put into action
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
}
}
else
{
cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
cvValidation.IsValid = false;
}
if (fuLogo.PostedFile.ContentLength > 409600 ) //File must be 400kb or smaller
{
cvValidation.ErrorMessage = "Please use a picture with a size less than 400 kb";
cvValidation.IsValid = false;
}
else
{
if (fuLogo.HasFile && cvValidation.IsValid)
{
fuLogo.SaveAs(logo); //Save the logo if file exists and Validation didn't fail. The path for the logo was created by the Server.MapPath() method.
}
}
}
else
{
logo = "N/A";
}
If you intend to save the files in
a directory on your web server , then
the Server.MapPath() will be the suitable
solution.
string dirPath = System.Web.HttpContext.Current.Server.MapPath("~") + "/Images/Logos/"+ fileBreak[0] + counter.ToString() + "." + fileBreak[1];
Look Here
if you intend to save your files out
the web server then
Use a full path, like "c:\uploads"
and be sure that the web process has
permission to write to that folder,I suggest you store the path itself in the web.config file in this case.
yes, that can be used after saving file and when you try retrieve that file...
Server.MapPath("~/Images/Logos/" + uploadedFileName);
Yes it should still work as Server.MapPath uses the relative values and returns the complete physical path.
It's one line of code:
FileUpload1.PostedFile.SaveAs(Server.MapPath(" ")+"\\YourImageDirectoryOnServer\\"+FileUpload1.FileName);
I have File Upload form which users use to upload certain files on the server. The files are uploaded in path that look like this.
"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
After that I have a repeater which displays some data and have a hyperlink. I want to name the hyperlink"Download files(" + fileCount + ")" and onclicked it should display a regular Save As window. Can you give me some code to do that. I've never done something like this.
To get the number of files in the directory, you need to use IO. Then
string path = #"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
int numFiles = Directory.GetFiles(path).Length;
Then in your page_load, just change to .Text of the link to include the numFiles variable