Django架構 之 Form表單和Ajax上傳檔案

來源:互聯網
上載者:User

標籤:space   ssd   frame   ext   lse   偽造   key   asc   RoCE   

瀏覽目錄
  • Form表單上傳檔案

  • Ajax上傳檔案

  • 偽造Ajax上傳檔案

 

Form表單上傳檔案html
1234567 <h3>form表單上傳檔案</h3>  <form action="/upload_file/" method="post" enctype="multipart/form-data">    <p><input type="file" name="upload_file_form"></p>    <input type="submit"></form>

注意:必須添加enctype="multipart/form-data屬性。

view
123456789 def index(request):     return render(request,"index.html")  def upload_file(request):    print("FILES:",request.FILES)    print("POST:",request.POST)    return HttpResponse("上傳成功!") 
Ajax上傳檔案什麼是FormData

XMLHttpRequest Level 2添加了一個新的介面FormData.利用FormData對象,我們可以通過JavaScript用一些索引值對來類比一系列表單控制項,我們還可以使用XMLHttpRequest的send()方法來非同步提交這個"表單".比起普通的ajax,使用FormData的最大優點就是我們可以非同步上傳一個二進位檔案.

所有主流瀏覽器的較新版本都已經支援這個對象了,比如Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。html
12345678910111213141516171819202122232425262728293031323334 <h3>Ajax上傳檔案</h3> <p><input type="text" name="username" id="username" placeholder="username"></p><p><input type="file" name="upload_file_ajax" id="upload_file_ajax"></p> <button id="upload_button">提交</button>{#注意button標籤不要用在form表單中使用#} <script>    $("#upload_button").click(function(){         var username=$("#username").val();        var upload_file=$("#upload_file_ajax")[0].files[0];         var formData=new FormData();        formData.append("username",username);        formData.append("upload_file_ajax",upload_file);          $.ajax({            url:"/upload_file/",            type:"POST",            data:formData,            contentType:false,            processData:false,             success:function(){                alert("上傳成功!")            }        });      })</script>
 注意:contentType:false,   processData:false, 必不可少。views.py 
123456789 def index(request):       return render(request,"index.html")      def upload_file(request):    print("FILES:",request.FILES)    print("POST:",request.POST)    return HttpResponse("上傳成功!") 
偽造Ajax上傳檔案iframe標籤

<iframe> 標籤規定一個內聯架構。

一個內聯架構被用來在當前 HTML 文檔中嵌入另一個文檔。

樣本:

1 <iframe src="http://www.baidu.com" width="1000px" height="600px"></iframe><em id="__mceDel" style=" font-family: ‘PingFang SC‘, ‘Helvetica Neue‘, Helvetica, Arial, sans-serif; font-size: 14px;">  </em>
iframe+form
123456789101112131415161718192021222324252627 <h3>偽造Ajax上傳檔案</h3><form action="/upload_file/" method="post" id="form2" target="ifr" enctype="multipart/form-data">    <p>        <iframe name="ifr" id="ifr"></iframe></p>    <p><input type="file" name="upload_file"></p>    <p><input type="text" name="user"></p>     <input type="button" value="提交" id="submitBtn"></form> <script>       $("#submitBtn").click(function(){         $("#ifr").load(iframeLoaded);        $("#form2").submit();      });     function iframeLoaded(){        alert(123)    } </script><em id="__mceDel" style=" font-family: ‘PingFang SC‘, ‘Helvetica Neue‘, Helvetica, Arial, sans-serif; font-size: 14px;">  </em>
views
12345678 def index(request):      return render(request,"index.html")  def upload_file(request):    print("FILES:",request.FILES)    print("POST:",request.POST)    return HttpResponse("上傳成功!")

Django架構 之 Form表單和Ajax上傳檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.