Home > php教程 > PHP开发 > Aajx file upload example with imitation iframe effect

Aajx file upload example with imitation iframe effect

高洛峰
Release: 2016-12-06 14:59:43
Original
1266 people have browsed it

Some time ago, I struggled for a while to solve the problem of ajax uploading files. There is definitely no problem in uploading text information directly using $.post. But it is not feasible to upload images directly in $.post.

Later I saw some solutions on the Internet, including ready-made ajax uploaded file encapsulation methods and some using flash. Flash is indeed a good method, but not everyone knows flash, and it is not easy to modify the downloaded ready-made method, and the file is relatively large. In the end, I had to simulate iframe to achieve it. Found it to be quite simple.

html:

<iframe name="ajaxUpload" style="display:none"></iframe>
<form name="from1" id="from1" method="post" action="url" enctype="multipart/form-data"target="ajaxUpload">
 <table>
  <tr>
    <td>附件:</td>
    <td><input type="file" id="document" name="document"/></td>
  </tr>
 </table>
 </form>
Copy after login

Here is the key point. To upload files, the enctype attribute is indispensable. The value of target is changed to the value of iframe's name.

Write the js code below. I use jQuery, so it is essential to load the jquery library when using it.

$(function(){
     if($.browser.msie){
       window.form1.submit();}else{
       $("#form1").submit();}
    });
Copy after login

Here is a browser version judgment, because IE is a browser that does not meet the specifications, especially IE6. IE6 does not directly support $("#idName").submit();.

The server is as follows, it has to return a value, direct submission cannot return a value

public void Upload()
{
  HttpPostedBase ff=Request.Files["document"];//这里是获取上传的文件流,也可以用索引值来表示如果是多个文件的话
  string fileName=System.DateTime.Now+ff.FileName.ToString();  //这里取出来的文件名是没有后缀的,所以要保存的话还需要取出文件拓展名。这里就不写过细,只是为描述这样一个思路。
  try
  {
    SaveAs(documentPath+fileName+extendtionName);
    Response.Write("<script type=&#39;text/javascript&#39; type=&#39;language&#39;>parent.window.callBackMethod(&#39;上传成功&#39;);</scrpt>");
  }
  catch
  {
    Response.Write("<script type=&#39;text/javascript&#39; type=&#39;language&#39;>parent.window.callBackMethod(&#39;上传失败&#39;);</scrpt>"); //parent.window.methodName();这个是JS调用父页的方法。因为现在模拟一个iframe上传文件,这个iframe的作用就是一个中间站的作用。在父页点击上传后通过target会将页面文档流传入iframe中再上传服务端作处理。服务端有响应之后然后再在iframe里面显示出来,而不是直接在父页面显示出结果。这里可能就是一个alert()弹出一个对话框提示一下,如果是这样那么不调父页方法也行。如果想把这提示的内容丰富一点比如弹出个类似人人网的蓝色的对话框之类的。
   }
}
Copy after login

Simulating an iframe is actually a partial update of the page, but the iframe in the page has no content and is not displayed, so it Refreshing will not affect the entire page at all.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template