file upload using iframe

 1 None.gif < html >
 2 None.gif < head >
 3 ExpandedBlockStart.gifContractedBlock.gif < script  type =”text/javascript” > dot.gif
 4InBlock.gifTotalFileFields = 5;
 5InBlock.gifStartUpload = false;
 6InBlock.gifCurrentFormID = 1;
 7InBlock.gif 
 8ExpandedSubBlockStart.gifContractedSubBlock.giffunction FilesUpload() dot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    while (1dot.gif{
10InBlock.gif        if (CurrentFormID > TotalFileFields) return true;
11ExpandedSubBlockStart.gifContractedSubBlock.gif        if (eval(’document.frm’ + CurrentFormID + ‘.upFile.value’) == ”) dot.gif{
12InBlock.gif            CurrentFormID++;
13InBlock.gif            continue;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        break;
16ExpandedSubBlockEnd.gif    }

17InBlock.gif    StartUpload = true;
18InBlock.gif    eval(’document.frm’ + CurrentFormID + ‘.submit()’);
19InBlock.gif    return false;
20ExpandedSubBlockEnd.gif}

21InBlock.gif 
22ExpandedSubBlockStart.gifContractedSubBlock.giffunction myIFrame_OnLoad() dot.gif{
23InBlock.gif    if (!StartUpload) return;
24InBlock.gif    CurrentFormID++;
25ExpandedSubBlockStart.gifContractedSubBlock.gif    if (FilesUpload()) dot.gif{
26InBlock.gif        StartUpload = false;
27InBlock.gif        CurrentFormID = 1;
28InBlock.gif        alert(’UPLOAD COMPLETE’);
29ExpandedSubBlockEnd.gif    }

30ExpandedSubBlockEnd.gif}

31InBlock.gif 
32ExpandedSubBlockStart.gifContractedSubBlock.giffunction GenerateUploadForms() dot.gif{
33ExpandedSubBlockStart.gifContractedSubBlock.gif    for (i = 1; i <= TotalFileFields; i++dot.gif{
34InBlock.gif        document.write(’<form name=”frm’ + i + ‘” method=”post” action=”http://localhost/” target=”myIFrame” enctype=”multipart/form-data”>’);
35InBlock.gif        document.write(’<input type=”file” name=”upFile”/></form>\n’);
36ExpandedSubBlockEnd.gif    }

37ExpandedBlockEnd.gif}

38None.gif
</ script >
39 None.gif </ head >
40 None.gif < body >
41 ExpandedBlockStart.gifContractedBlock.gif < script  type =”text/javascript” > dot.gif GenerateUploadForms(); </ script >
42 None.gif < input  type =”button”  value =”UPLOAD”  onclick =”FilesUpload();” />
43 None.gif < iframe  id =”myIFrame”  name =”myIFrame”  onload =”myIFrame_OnLoad();”  style =”display:none;” ></ iframe >
44 None.gif

http://vinayakshrestha.wordpress.com/2007/03/20/javascript-multiple-files-upload-trick/

 1 None.gif //creating the iframe
 2 None.gif var iframe = document.createElement("iframe");
 3 None.gif iframe.setAttribute("id","micox-temp");
 4 None.gif iframe.setAttribute("name","micox-temp");
 5 None.gif iframe.setAttribute("width","0");
 6 None.gif iframe.setAttribute("height","0");
 7 None.gif iframe.setAttribute("border","0");
 8 None.gif iframe.setAttribute("style","width: 0; height: 0; border: none;");
 9 None.gif 
10 None.gif //add to document
11 None.gif form.parentNode.appendChild(iframe);
12 None.gif window.frames['micox-temp'].name="micox-temp"; //ie sucks

http://elmicoxcodes.blogspot.com/2007/03/asynchronous-upload-like-ajax-1.html

Ajax extensions does not support file upload. So, what I have done is create an iframe and put a file upload form inside that iframe. When the upload is complete, the event can then be relayed to the ajax trigger events and do additional tasks in ordinary ajax coding.

This method can also be made GMail like. You can control anything of the iframe using javascript. And hence you can do something like IFrame.forms[0].submit() in javascript to automatically start files uploading (and as it is iframe, it would not look like form post). You can also easily poll the status of the iframe, so that if user clicks other form buttons in your page, you can wait for iframe to complete upload and then finally do other task.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值