0

I have a AJAX File Uploader for Profile Image Upload. I want to restrict the user to upload image upto 102400 Bytes only.How to display javascript error message when file size exceeds.

Code:

protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
    string workerfile = Session["LoginUserID"].ToString();
            string strFileName = string.Empty;
            string strFileType = string.Empty;
            string path = string.Empty;
            var folder = Server.MapPath("../worker/Uploads/" + workerfile);
    if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
                strFileName = DateTime.Now.ToString("MM-dd-yyyy_HHmmss");
                strFileType = System.IO.Path.GetExtension(e.FileName).ToString().ToLower();

                AjaxFileUpload1.SaveAs(Server.MapPath("../worker/Uploads/" + Session["LoginUserID"] + "/" + workerfile + "_img" + strFileType));
            }
            else
            {

                strFileName = DateTime.Now.ToString("MM-dd-yyyy_HHmmss");
                strFileType = System.IO.Path.GetExtension(e.FileName).ToString().ToLower();


                AjaxFileUpload1.SaveAs(Server.MapPath("../worker/Uploads/" + Session["LoginUserID"] + "/" + workerfile + "_img" + strFileType));

            }
}

How to Do any Idea? Help Appreciated.

1 Answer 1

0

Unfortunately there is no way to respond to the client with details on what went wrong server side - that is to say, there is no formal way to do so. Here's an alternative:

  1. Check the file size using AjaxFileUploadEventArgs.FileSize. If it exceeds your maximum (it looks like you may be trying to limit to 1 MB, the correct value is 1048576), set AjaxFileUploadEventArgs.PostedUrl = "Your file is too big, etc."

  2. Set the OnClientUploadComplete property on your control to the name of a JS function that will either show the image if the file name string is formatted as such, or show a window with the error message.

It's a hack of course, but it'll work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.