收录日期:2019/10/22 14:51:00 时间:2010-08-05 15:02:35 标签:wcf,http,upload

thanks for clicking.

I am trying to get status feedback using an IFrame for file uploads. I am not trying to get progress or percentages - just when a file is done uploading and if it was a success or failure.

THE PROBLEM is that I can't seem to get the server response to appear on the client. I have to following design:

I have an iframe on my page:

 <iframe id="target_frame" src="" style="border:0px; width:0px; height:0px"></iframe>

The form tag points to it:

<form enctype="multipart/form-data" id="fileUploadForm" name="fileUploadForm" action="picupload.aspx" method="post" target="target_frame">

And the submit button starts a file upload via the iframe:

<input id="submit" type="submit" value="upload" />

In the picupload.aspx.cs file, I have a method that returns dynamic data. I then send it to the client:

message = data;
Response.Write(String.Format("<script language='javascript' type='text/javascript'>window.parent.handleResponse('{0}');</script>", message));

On the client, I have a response handler:

 function handleResponse(msg) {
        document.getElementById('statusDiv').innerHTML = msg;
  }

My intent is to see the msg value change for each uploaded file but I never see anything appear in statusDiv, let alone dynamically changing messages.

Can somebody please help??

I've been unable to find a way to detect an iframe's server response, but it looks like the purpose of your iframe is just to show the results of the form submission request without a page refresh. If so, then maybe you can replace the iframe with an XHR call.

I'd recommend using some flavor of Ajax library for simplicity, but I've had a few scenarios where that wasn't an option for one reason or another. Here's a toy example of how to make an XHR form submission request using Javascript without AJAX.

NOTE: This example assumes the form is not sending file data (that's a whole other kettle of fish, and much better handled with AJAX). It also won't submit the values of <select> or <textarea> tags, but you could certainly extend the javascript to handle those.

/* HTML */
<form action="script.php" method="post"   // these are up to you
    onsubmit="sendFormRequest(this); return false;">

/* Javascript */
function sendFormRequest(form){
    var fields = form.getElementsByTagName('input');
    var data = [];
    for(var i=0; i<fields.length; ++i){
        if(fields[i].getAttribute('type') != 'submit')
            data[i] = escape(fields[i].name) + '=' + escape(fields[i].value);
    }
    var req = new XMLHttpRequest();
    req.open(form.getAttribute('method'), form.getAttribute('action'), false);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(data.join('&'));
    return {'status':req.status, 'responseBody':req.responseText};
}

/* Returns */
Object { status=200, responseBody="<!DOCTYPE html PUB ... \n</body>\n</html>\n" }

I doubt this post is still relevant, I'm mainly replying to it because it comes up in search results and doesn't seem to have a quick, usable answer.

Until you provide more code, I can only guess at the issue. I would first check to make sure that the iframe page (picupload.aspx.cs) is host on the same domain as the page that contains the iframe.

Hope this helps:

http://www.nbsp.es/2007/11/23/up-libreria-para-subir-archivos-de-forma-oculta/

Help is in spanish, but the code is there.