Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
Using file upload control,im uploading multiple files. Im saving the files in a local folder. If three files are uploaded at a time,all the three are saved but the third file replaced the first two files so that only third file can be viewed there... This is my cs code
C#
HttpFileCollection multiplefiles = Request.Files;
            string virtualPath = "~/Uploads";
            string physicalFolder = Server.MapPath(virtualPath);
            for (int fileCount = 0; fileCount < multiplefiles.Count; fileCount++)
            {
                HttpPostedFile uploadFile = multiplefiles[fileCount];
                string fileName = Path.GetFileName(uploadFile.FileName);
                if (uploadFile.ContentLength > 0)
                {
                    uploadFile.SaveAs(physicalFolder + FileUpload1.FileName);
                    lbl2.Text += fileName + "Saved<br />";
                }
            }


Here Uploads is the virtual path folder created to save the uploaded files. Guide me soon...
Posted
Updated 21-Aug-12 0:01am
v2

Hi,

There are many ways to get your work done,

Here i am giving you one solution. you need to append GUID/Timestamp/Random with your uploaded file and you also need to store the actual file name.

When you are storing your file in the database, you need to add one more column with actual file name and store the file location with modified file name.

Here i am giving you one example of how you can append TimeStamp.

C#
string fileName = string.Format("{0}{1}", uploadFile.FileName,DateTime.Now.ToString("yyyyMMddHHmmssffff"));


And at the time of download you need to give that file for download with the same name that you have stored in database.

Hope this simple way help you.

Thanks
-Amit Gajjar
 
Share this answer
 
this will work......

C#
HttpFileCollection multiplefiles = Request.Files;
            string virtualPath = "~/Uploads";
            string physicalFolder = Server.MapPath(virtualPath);
            for (int fileCount = 0; fileCount < multiplefiles.Count; fileCount++)
            {
                HttpPostedFile uploadFile = multiplefiles[fileCount];
                string fileName = Path.GetFileName(uploadFile.FileName);
                if (uploadFile.ContentLength > 0)
                {
                    uploadFile.SaveAs(physicalFolder + fileName);
                    lbl2.Text += fileName + "Saved<br />";
                }
            }


u made an fault here... I think FileUpload1 is ur third file uploader id!!!

uploadFile.SaveAs(physicalFolder + fileName);
 
Share this answer
 
v3
Comments
fjdiewornncalwe 21-Aug-12 8:59am    
No downvote from me, but a suggestion. Could you please not use textspeak when you write things here. It is very annoying to members here to read that.
just take random number and rename the file with random number and add image1, image2, image3 names with that random number,,, then your problem may get solved..

go through code bellow...


string image1=""
Random ran = new Random();
string ra_One = "Image_1" + ran.Next(10000, 99999).ToString();
image1 = txt_EventName.Text.Trim() + ra_One;
 
string PIfilePath = string.Empty;
string PIfileName = Path.GetFileName(fu_Image1.FileName);
string PIfileExt = Path.GetExtension(PIfileName).ToLower();
PIfilePath = image1.Replace("/", "-") + PIfileExt;
image1 = PIfilePath;
fu_Image1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + image1))


...
 
Share this answer
 
You need to rename the file to some unique name (cannot be repeated) before you do uploadFile.SaveAs.

The unique name can be obtained using
GUID
DATE TIMESTAMP

cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900