Ajax Image file upload

User-1581580067 posted

Hi Everyone,

I am trying to accomplish this. Users can upload 5 photos. Once they upload, thumbnail will be shown on the page and they can delete as well. The complication happening here is the combination of Ajax (WebMethod) and Server Control.

Javascript as follow

function addElement(){document.getElementById("imageID").value = PageMethods.UploadImage();}

HTML code as follow

 <asp:FileUpload runat="server" ID="imageUpload" />                        <input type="button" id="imageUploadButton" onclick="javascript:addElement();" value="Upload" />

Web method as follow

    [System.Web.Services.WebMethod()]    public static String UploadImage()    {        if (imageUpload.HasFile && imageUpload.PostedFile != null) //I can't use imageUpload since it's server side control        {            try            {                   //do whatever you have to do.            }            catch (Exception exec)            {            }        }        return "";    }

When upload button clicked, it will called addElement (javascript method) then again it will call code-behind method. Since, the code-behind method is a static method, there is no way that I can make a reference back to the imageUpload control. I believe
there will be a workaround and I would be appreciated if anyone would give me input on this. Thanks

Xổ số miền Bắc