当jquery uploadify上传时,重命名httphandler中的文件

本文关键字:httphandler 重命名 文件 jquery uploadify | 更新日期: 2023-09-27 18:22:20

我想要通过httphanlder在ASP.Net中使用jQuery Uploadify进行多文件上传。

当不更改名称文件时,很好地工作并上传所有文件,当我想在httphandler中更改文件名时,许多文件不上传并中断。

asp.net代码为:

$("#<%=FileUpload1.ClientID %>").fileUpload({
        'uploader': '../js/uploader.swf',
        'cancelImg': '../images/cancel.png',
        'buttonText': 'Browse Files',
        'script': 'Upload.ashx',
        'folder': '../FileUpload/AttachmentFiles/' + userID,
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'multi': true,
        'auto': true,
        'onUploadProgress': function (file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
            $('#progress').html(totalBytesUploaded + ' bytes uploaded of ' + totalBytesTotal + ' bytes.');
        }
                , onComplete: function (event, queueID, fileObj, response, data) {
                    debugger;
                    var onclick = 'FileDelete("' + queueID + '")';
                    var ul = $('#AttchmentFiles');
                    var li = '<li data-ID=' + queueID + '><a><img onclick=' + onclick + ' src="Template/Images/Remove.png" width="20" height="20" /></a><a><img src="' + response + '" width="30" height="30" style="top: 0;" /></a>';
                    ul.append(li);
                }
    });

httphandler文件代码:

public class Upload : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
            string savepath = "";
            string tempPath = "";
            tempPath = HttpContext.Current.Request.QueryString["folder"];
            savepath = context.Server.MapPath(tempPath);
            string filename = FileName(postedFile);
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);
        postedFile.SaveAs(savepath + @"'" + filename);
        context.Response.Write(tempPath + "/" + filename);
        context.Response.StatusCode = 200;
    }
    catch (Exception ex)
    {
        context.Response.Write("Error: " + ex.Message);
    }
}
public string FileName(HttpPostedFile postedFile) {
    string Extention = Path.GetExtension(postedFile.FileName);
    string randstr = "";
    if (Extention.ToLower() == ".jpg" || Extention.ToLower() == ".png" || Extention.ToLower() == ".jpeg" || Extention.ToLower() == ".gif")
    {
        Random rand = new Random();
        int Intrandom1 = rand.Next(100000000, 999999999);
        int Intrandom2 = rand.Next(100000000, 999999999);
        int Intrandom3 = rand.Next(100000000, 999999999);
        randstr = "Img" + Intrandom1.ToString() + Intrandom2.ToString() + Intrandom3.ToString() + ".jpg";
    }
    else if (Extention.ToLower() == ".fla" || Extention.ToLower() == ".mp4" || Extention.ToLower() == ".flv")
    {
        Random rand = new Random();
        int Intrandom1 = rand.Next(100000000, 999999999);
        int Intrandom2 = rand.Next(100000000, 999999999);
        int Intrandom3 = rand.Next(100000000, 999999999);
        randstr = "movie" + Intrandom1.ToString() + Intrandom2.ToString() + Intrandom3.ToString() + ".flv";
    }
        //'*.pdf;*.zip;*.rar;*.txt;*.docx',
    else if (Extention.ToLower() == ".pdf" || Extention.ToLower() == ".zip" || Extention.ToLower() == ".rar" || Extention.ToLower() == ".txt" || Extention.ToLower() == ".docx")
    {
        Random rand = new Random();
        int Intrandom1 = rand.Next(100000000, 999999999);
        int Intrandom2 = rand.Next(100000000, 999999999);
        int Intrandom3 = rand.Next(100000000, 999999999);
        randstr = "file" + Intrandom1.ToString() + Intrandom2.ToString() + Intrandom3.ToString() + Extention.ToLower();
    }
    return randstr;
}
public bool IsReusable
{
    get
    {
        return false;
    }
}

}

当jquery uploadify上传时,重命名httphandler中的文件

这是因为以下内容将始终为每个文件生成相同的输出,这将导致所有文件都使用相同的文件名编写。第一个上传的文件会成功,但其余大部分文件在保存过程中都会失败,因为目标文件正在使用中,其中一些文件会成功但实际上会覆盖以前上传的文件。

Random rand = new Random();
int Intrandom1 = rand.Next(100000000, 999999999);
int Intrandom2 = rand.Next(100000000, 999999999);
int Intrandom3 = rand.Next(100000000, 999999999);

你为什么不改为:

randstr = "Img" + Guid.NewGuid().ToString() + ".jpg";

这是我的测试:

Random rand = new Random();
int Intrandom1 = rand.Next(100000000, 999999999);
int Intrandom2 = rand.Next(100000000, 999999999);
int Intrandom3 = rand.Next(100000000, 999999999);
Console.WriteLine("{0}{1}{2}", Intrandom1, Intrandom2, Intrandom3);
rand = new Random();
Intrandom1 = rand.Next(100000000, 999999999);
Intrandom2 = rand.Next(100000000, 999999999);
Intrandom3 = rand.Next(100000000, 999999999);
Console.WriteLine("{0}{1}{2}", Intrandom1, Intrandom2, Intrandom3);

以下是我的结果:

288925957582541325412731421
288925957582541325412731421