0

I have created an user control as follows to upload images. In it's code behind I have the method to save the image.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCImageUploader.ascx.cs"
    Inherits="WebApplicationFor_ABC.UCImageUploader" %>
<div>
    <asp:FileUpload ID="FileUploadImage" runat="server" />
    <asp:Image ID="ImageThumbnail" runat="server" />
    <asp:Label ID="LblMsg" runat="server" Text=""></asp:Label>
<asp:CheckBox ID="CheckBoxDelete" runat="server" Text="Delete"/>

</div>

code behind as follows

 public partial class UCImageUploader : System.Web.UI.UserControl
    {
        public bool SaveImg()
        {
                        //file field isn't empty
        if (FileUploadImage.PostedFile != null)
        {

            //check file size
            HttpPostedFile img = FileUploadImage.PostedFile;

            if (img.ContentLength == 0) // no file uploaded
            {
                msg = "No file was uploaded";
                LblMsg.Text = msg;
                return false;
            }
            else if (img.ContentLength > fileSizeInKB * 1024) // file size exceeds limit
            {
                msg = "File size exceeds the limit. Max is " + fileSizeInKB + "KB";
                LblMsg.Text = msg;
                return false;
            }
            else // accepted file size
            {
                //check file extension jpg,jpeg,BMP

                string[] fileExtensions = { "jpg", "jpeg", "bmp" };

                if (!fileExtensions.Contains(Path.GetExtension(img.FileName).ToLower()))
                {
                    msg = "File extension must be of jpg,jpeg,bmp";
                    LblMsg.Text = msg;
                    return false;
                }

            }
        }
        msg = "Valid File";
        LblMsg.Text = msg;
        return valid;
    }

On another button's click event I want to call this SaveImg() method and save the Image. The problem is because of the postback image returns as null. How can i fix this.

1
  • The problem should be in the SaveImg method (or who knows). Without sharing it I believe no one will be able to solve your problem. Commented Dec 4, 2012 at 11:17

1 Answer 1

1

In order to access the image, you do not do this through the file upload control, e.g.

FileUploadImage.SaveAs(savePath);
Sign up to request clarification or add additional context in comments.

1 Comment

Then how can I call the method defined in the control? Is there anyway I can call this method and access the data passed without losing them between the postbacks..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.