I build a Web Form with ASP.NET and VB.NET.
After uploading an image with AsyncFileUpload, the uploaded image should be shown in an image box. The AsyncFileUpload doesn't raise a page postback, therefore I have to handle this on client side by JavaScript.
Question : How can I get the upload file path from AsyncFileUpload by JavaScript ?
<asp:Image ID="imgCategory" runat="server" Height="225px" Width="300px" />
<ajaxToolkit:AsyncFileUpload ID="ajxAsyncFileUpload" runat="server" OnClientUploadComplete="UploadComplete"/>
<script type="text/javascript">
function UploadComplete() {
var uploadpath = document.getElementById('<%=ajxAsyncFileUpload.ClientID%>').???????;
document.getElementById('<%=imgCategory.ClientID%>').setAttribute('src', uploadpath);
}
</script>
Use of any file upload never passes to the server the client-side path name.
As for no post-back when the uploading of files is done?
Well, there is a client side "all files done uploading" event, and hence you can use that to trigger a post back, and even better is to have that routine "click" a button, so then server side you have a nice code stub for when all files uploaded on done.
So, say this simple markup:
So, note how we have a "hidden" button, and when all files up-loaded are done, then we click that button. That gets us a final code behind stub to run when all the files are uploaded.
So, code behind is this:
And for a "nice touch" and bonus points? We display file types or an image preview by using the row data bound event of the GridView.
Hence this code:
The end result is now this:
So, in most cases, you don't get nor have a path name passed client side. However, since each file upload triggers the UpLoadComplete event, then we save the file to some folder, and we for this example added a row to a database.
And our "hidden" button that we click from the client-side JavaScript routine when all files done uploading? That is this simple code:
I often use a RadioButtonList as a "tab like" control, and just hide or show the 2 divs for the uploading part, or the viewing of uploaded files.