Saturday, March 24, 2012

Uploading file during onchange

I try to upload file in server during user select file:

<inputid="iupload"runat="server" type="file"style="width:1px"/>
<inputtype="hidden"id="hdName"runat="server"/>

<scriptlanguage="javascript">
function Doo(){
document.getElementById('hdName').value = document.getElementById('iupload').value;// to check path
WebForm_DoCallback('__Page',document.getElementById('iupload').value,CallbackFunction,'context',null,false);returnfalse;
}
</script>

and on server side:

protectedvoid Page_Load(object sender, EventArgs e)
{
string argClientFunction ="";
string cScript = ClientScript.GetCallbackEventReference(this, argClientFunction,"CallbackFunction","'context'","null",false);
string f=@dotnet.itags.org."
javascript:Doo();";
iupload.Attributes.Add("onchange", f);

string scr ="<script language=javascript>";
scr +="function CallbackFunction(result, context)";
scr +="{";
scr +="document.getElementById('spUploaded').innerText += 'Done';";
scr +=" return false;";
scr +="}";
scr +="</script>";
RegisterStartupScript("scr" +this.ClientID, scr);

}


publicvoid UploadFile(string fileName)
{
// try to upload file to server
}
publicstring GetCallbackResult()
{
UploadFile(evArg);

evArg = String.Empty;

return evArg;
}
publicvoid RaiseCallbackEvent(string eventArgument)
{
evArg = eventArgument;
}

Problem occur in UploadFile(...): When I try to get iupload.PostedFile for file uploading it's null. I can get file name only.

How can I upload file normally without postback with onchange ?

I need upload file to server as Gmail or Yahoo Mail!

File uploads require a full postback. There has been a lot of talk on here about sites such as gmail that appear to asynchronously load a file. At this point, the only way that I have heard of this successfully implemented is by using IFrames and have another page that does the full postback, but doesn't look like it.


Thank you! I have heard about Iftame to upload files. Have you any information about this?


Not really... we haven't got into handling file uploads very often, and if we do, we would probably use the telerik fileupload control since we have the suite and I believe it handles very large files much better that the built in fileupload for asp.net. When I tested both of them, I was able to crash IIS with the built in control by trying to upload a dvd image because it seemed to check for the file size only after it had been processed through IIS, but the telerik control seemed to do that in pieces which didn't cause it to lock up.

No comments:

Post a Comment