使用文件上传控件上传多个文件时,如何避免替换文件 [英] How to avoid replacing of files when more than one file uploaded using file upload control

查看:90
本文介绍了使用文件上传控件上传多个文件时,如何避免替换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..
使用文件上传控制,即可以上传多个文件.我将文件保存在本地文件夹中.如果一次上传三个文件,则所有三个文件均被保存,但第三个文件替换了前两个文件,因此只能在其中查看第三个文件...这是我的CS代码

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

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 />";
                }
            }



这里的Uploads是创建的虚拟路径文件夹,用于保存上载的文件.很快为我提供指导...



Here Uploads is the virtual path folder created to save the uploaded files. Guide me soon...

推荐答案



有很多方法可以完成工作,

在这里,我给你一个解决方案.您需要在上传的文件后附加 GUID/Timestamp/Random ,还需要存储实际的文件名.

将文件存储在数据库中时,您需要再添加一列带有实际文件名的文件,并以修改后的文件名存储文件的位置.

在这里,我给您提供一个示例,说明如何添加TimeStamp.

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.

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



并且在下载时,您需要使用与数据库中存储的名称相同的名称来下载该文件.

希望这种简单的方法对您有所帮助.

谢谢
-Amit Gajjar



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


这将起作用……

this will work......

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 />";
                }
            }



您在这里犯了一个错误...我认为 FileUpload1 是您的第三个文件上传者ID!
uploadFile.SaveAs(physicalFolder + fileName);



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

uploadFile.SaveAs(physicalFolder + fileName);


只取随机数并使用随机数重命名文件,然后添加具有该随机数的image1,image2,image3名称,则可能是您的问题得到解决..

通过下面的代码...


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



...



...


这篇关于使用文件上传控件上传多个文件时,如何避免替换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆