You must Sign In to post a response.
  • Category: ASP.NET

    Keeping status of fileupload control

    I am having File upload control to upload image in registration form and i would like to check the size and type of image loaded when the image is coming to the image control . Hows it possible?
    Also i am using the code as follows:
    staffphoto is the id of fileupload control(the problem is for every postback the image disappears and go back to nofile selected)

    if (staffphoto.HasFile)
    {


    try
    {
    if (staffphoto.PostedFile.ContentType == "image/jpeg")
    {
    if (staffphoto.PostedFile.ContentLength < 102400)
    {

    // filepath = Server.MapPath("~/upload_staff_photos/") + stid + ".jpeg";
    filepath = Server.MapPath("~/upload_staff_photos/");
    staffphoto.SaveAs(filepath);
    // imgstaff.ImageUrl = "~/upload_staff_photos/" + stid + ".jpeg";
    imgstaff.ImageUrl= "~/upload_staff_photos/"+filename;
    StatusLabel.Text = "Upload status: File uploaded!";
    stbasic.execquery("update staff_mast set staff_pick_url='" + imgstaff.ImageUrl + "'");

    }
    else
    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
    }
    else
    StatusLabel.Text = "Upload status: Only JPEG files are accepted!";

    }
    catch (Exception ex)
    {
    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
    }
    }
  • #762248
    Hello Avanika Sankar,

    //If first time page is submitted and we have file in FileUpload control but not in session
    // Store the values to SEssion Object
    if (Session["FileUpload1"] == null && FileUpload1.HasFile)
    {
    Session["FileUpload1"] = FileUpload1;
    Label1.Text = FileUpload1.FileName;
    }


    // Next time submit and Session has values but FileUpload is Blank
    // Return the values from session to FileUpload
    else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile))
    {
    FileUpload1 = (FileUpload) Session["FileUpload1"];
    Label1.Text = FileUpload1.FileName;
    }


    // Now there could be another situation when Session has File but user want to change the file
    // In this case we have to change the file in session object
    else if (FileUpload1.HasFile)
    {
    Session["FileUpload1"] = FileUpload1;
    Label1.Text = FileUpload1.FileName;
    }

    You can refer the below mentioned link :

    http://www.aspsnippets.com/Articles/Display-image-after-upload-without-page-refresh-or-postback-using-ASP.Net-AsyncFileUpload-Control.aspx


    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments