通过 jquery uploadify 上传时在 httphandler 中重命名文件

rename files in httphandler when upload by jquery uploadify

我想通过 httphanlder 在 ASP.Net 中使用 jQuery 上传多个文件。

当不更改文件名时,工作正常并上传所有文件,当我想在 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;
    }
}

}

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

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