locked
Whats wrong on my file upload function on asp.net RRS feed

  • Question

  • User1741069310 posted
    if (Photo.HasFile)
    {
    try
    {
    Boolean fileOK = false;
    string pathToFiles = Server.MapPath("/Uploads");
    String path = Server.MapPath("~/");
    String fileExtension = System.IO.Path.GetExtension(Photo.FileName).ToLower();
    String[] allowedExtensions = {".gif", ".png", ".jpeg", ".jpg"};
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
    if (fileExtension == allowedExtensions[i])
    {
    fileOK = true;
    }
    }
    if (fileOK)
    {
    try
    {
    Photo.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + Photo.FileName);
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('File uploaded')", true);
    }
    catch (Exception ex)
    {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "File could not be uploaded" + "')", true);
    }
    }
    else
    {
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "Cannot accept files of this type." + "')", true);
    }
    }
    catch (Exception ex)
    {
    // StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
    }
    }
    // Response.Redirect("./Default2.aspx");
    }
    Wednesday, November 14, 2018 4:33 AM

All replies

  • User-821857111 posted

    What makes you think anything is wrong with it? Do you get errors? If so, what are they? If not, what happens and what do you expect to happen?

    Wednesday, November 14, 2018 12:02 PM
  • User753101303 posted

    Hi,

    You see a "File could not be uploaded" message ? Don't just forget about the exception. Showing a generic message to the user is fine but you should log exception details so that you can see what happens.

    It's easier to fix your code by starting from the actual error that happens rather than by reading code (even simple code could fail for multiple reasons).

    Wednesday, November 14, 2018 12:19 PM
  • User61956409 posted

    Hi fsze88,

    I do a test with the code that you provided for validating files using their extensions and uploading only image files, the code work fine on my side.

    <asp:FileUpload ID="Photo" runat="server" />
    <asp:Button ID="btnupload" runat="server" Text="upload" OnClick="btnupload_Click" />
    protected void btnupload_Click(object sender, EventArgs e)
    {
        if (Photo.HasFile)
        {
            try
            {
                Boolean fileOK = false;
                string pathToFiles = Server.MapPath("/Uploads");
                String path = Server.MapPath("~/");
                String fileExtension = System.IO.Path.GetExtension(Photo.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
    
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
    
                if (fileOK)
                {
                    try
                    {
                        Photo.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + Photo.FileName);
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('File uploaded')", true);
    
                    }
                    catch (Exception ex)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "File could not be uploaded" + "')", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + "Cannot accept files of this type." + "')", true);
                }
    
            }
            catch (Exception ex)
            {
                // StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
        // Response.Redirect("./Default2.aspx");
    }

    Test Result:

    If you get any exception while executing your code, please clarify more about the problem and share the exception details, so that we can understand the question better.

    With Regards,

    Fei Han

    Thursday, November 15, 2018 8:00 AM