Hoorakhsh
Hoorakhsh

Reputation: 61

rename files in httphandler when upload by jquery uploadify

i want Multiple File Upload with jQuery Uploadify in ASP.Net by httphanlder.

when dont change name files good work and upload all files,when i want change file name in httphandler many files dont upload and break.

asp.net Code is:

$("#<%=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 file code:

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

}

Upvotes: 0

Views: 608

Answers (1)

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

That is because the following will always generate the same output for every file and this causes all the files to be written with the same file name. The first uploaded file will succeed, but most of the rest will fail during save because the destination file would be in use, and, some of them will succeed but actually will overwrite previously uploaded ones.

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

Why don't you instead change it like:

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

Here is my test:

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

And here are my results:

288925957582541325412731421
288925957582541325412731421

Upvotes: 1

Related Questions