0

How can I call this Javascript function from the code-behind without refreshing the page?

function fileUploaded(fileName) {
    // Send FileName off to AJAX call to do something with it
}

... I'm using the FileUpload .Net component in the .aspx to upload multiple files all at once:

<form runat="server">
<asp:FileUpload runat="sever" id="FileUpload1" AllowMultiple="true"/>
<asp:Button runat="server" OnClick="UploadMultipleFiles"/>
</form>

... In the code-behind I'm looping through each of the posted files and saving them on the server one at a time. After each file is saved, I'm trying to call the Javascript function, fileUploaded, on the client side:

protected void UploadMultipleFiles(object sender, EventArgs e) {
FileUpload FileUpload2 = (FileUpload)this.FindControl(FileUpload1);
if (FileUpload2.HasFiles) 
{ 
    foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles) 
    {
        fileName = Path.GetFileName(postedFile.FileName);
        postedFile.SaveAs(lblPath + fileName); 
        // Used ScriptManager.RegisterStartupScript too
        ClientScript.RegisterStartupScript(this.GetType(), 
                       "fileUploaded" + _intScriptIndex++, 
                       "fileUploaded('" + fileName + "');", true); 
    } 
}

... The "Problem" is that the code behind doesn't call the Javascript function until all files have been uploaded. It reloads the page with the following appended to the end of the DOM:

<script type="text/javascript">
//<![CDATA[
fileUploaded('00007.MTS');fileUploaded('00009.MTS');fileUploaded('00012.MTS');//]]>
</script>

The "Question" is, How do I call the Javascript function fileUploaded from the code-behind immediately after each file is saved without reloading the page?

Velocedge
  • 1,222
  • 1
  • 11
  • 35
  • 1
    Sounds like you need to do some client side work, e.g. https://stackoverflow.com/questions/10042184/jquery-file-upload-is-it-possible-to-trigger-upload-with-a-submit-button – MatthewMartin Feb 16 '16 at 21:28
  • @MatthewMartin I don't think thats going to, from what I understand the op is uploading multiple files from a single file select and wants an update as the server get them – johnny 5 Feb 16 '16 at 21:31
  • OP probably needs something like http://www.dropzonejs.com/ or http://fineuploader.com/ and client-side work and a http endpoint for accepting the files themselves – Chris Marisic Feb 16 '16 at 21:33
  • 2
    I can't tell what is going on. Multiple file uploads are sent in a single POST. Under the covers it's still all HTTP, stateless and two different machines. Server side activities can't trigger events on the client except via a Respose. – MatthewMartin Feb 16 '16 at 21:33
  • if (FileUpload2.HasFiles) { foreach (HttpPostedFile postedFile in FileUpload2.PostedFiles) { fileName = Path.GetFileName(postedFile.FileName); postedFile.SaveAs(lblPath + fileName); ClientScript.RegisterStartupScript(this.GetType(), "fileUploaded" + _intScriptIndex++, "fileUploaded('" + fileName + "');", true); } } – Velocedge Feb 16 '16 at 21:36
  • @Velocedge so they are seperate requests? – johnny 5 Feb 16 '16 at 21:37
  • @MatthewMartin he can do it if he parses out each file as a seperate request, or if he wanted he could probably open a socket an create his upload action as async and send back updates as each one saves to a directory. But that seems like overkill for some feature that probably isn't important what so ever like a progress bar – johnny 5 Feb 16 '16 at 21:37
  • Sorry... that accidentally added before I could get it formatted. I'm picking multiple files from my source (camera) and uploading them to the server using the .Net FileUpload component. The results of the above code put the following in the DOM. Johnny, I select all files at once with the file dialog and that foreach loop processes them one at a time. – Velocedge Feb 16 '16 at 21:40
  • @Velocedge it seems like your trying to do it from the server, in which case if you want easiest access to when they are uploaded parse out each file as its own request and when it return use its call back to do what ever you need to do. Other wise you'll have to do some really weird complicated solution thats unneeded – johnny 5 Feb 16 '16 at 21:40
  • MathewMartin has the right idea – Velocedge Feb 16 '16 at 21:44
  • @Velocedge the first code you posted is from there server side which seems like your processing multiple files at a time, the javascript code you posted seems like it is uploading a file one at a time so which is it? – johnny 5 Feb 16 '16 at 21:44
  • Client side is a regular Javascript function called fileUpload(fileName) which takes the fileName and sends it off to be processed. The Javascript I posted is what the code-behind physically inserts into the DOM when all files have been uploaded, calling the fileUpload 3 times for the 3 files it uploaded. – Velocedge Feb 16 '16 at 21:49

0 Answers0