.net批量上传命名冲突问题~~~在线等!

来源:百度知道 编辑:UC知道 时间:2024/06/02 09:27:15
protected void BtnUpFiles_Click(object sender, EventArgs e)
{
StringBuilder strMessage = new StringBuilder();
HttpFileCollection Files = HttpContext.Current.Request.Files;
for (int i = 0; i < Files.Count;i++ )
{
HttpPostedFile PostedFile = Files[i];
if (PostedFile.ContentLength>0)
{
string FileName = PostedFile.FileName;
string strExPrentFile = FileName.Substring(FileName.LastIndexOf(".") + 1);
string[] NoExPrentFile = new string[] { "exe", "asp", "php" };
bool IsUp = true;
for (int j = 0; j < NoExPrentFile.Length; j++)
{
if (strExPrentFile.Equals(NoExPrentFile[j]))
{
IsUp = false;
}

sFilePath=路径+文件名

解决的方法:文件名你用一个随机字符串代替文件实际名称,即可解决你的问题!sFilePath=路径+随机字符串+文件扩展名,构造完成后,执行PostedFile.SaveAs(Server.MapPath(sFilePath));就可以了。

fileExtension = System.IO.Path.GetExtension(fileName);//获取扩展名
fileCodeName = GenRndString(50) + fileExtension;//构造新文件名
GenRndString(50)//自己要写的随机字符串方法

在文件名后面加一个随机数!就这么都不会重复了!

Random rnd = new Random();
int n = rnd.Next(1000, 9999);

n.toString()一下!拼接到你上传文件的文件名后面就OK哈!

DateTime.Now.ToString()这样就可以了重复率就底了

怎么可能同时?直接命名DateTime.Now.ToString();