django 上传文件

views:

def subfile(request):
    if request.method == "POST":
        myFile =request.FILES.get("myfile", None)
        if not myFile:
            returnHttpResponse("no files for upload!")
        destination = open(os.path.join("/opt/bp/",myFile.name),'wb+')
        for chunk in myFile.chunks():
            destination.write(chunk)
        destination.close()
        return HttpResponse("upload over!")

 

html:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
  <form enctype="multipart/form-data" action="http://127.0.0.1:8814/subfile/" method="post">
   <input type="file" name="myfile" />
   <br/>
   <input type="submit" value="upload"/>
</form>
 </body>
</html>

 

urls.py:

from django.conf.urls import include, url

from django.contrib import admin

from . import views as k

urlpatterns = [

url(r'subfile', k.subfile)

]

 

 

 

 参考:

https://www.cnblogs.com/nulige/p/6582355.html

https://www.cnblogs.com/huchong/p/7910131.html

 

相关文章:

  • 2021-08-21
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-18
  • 2021-04-27
  • 2021-07-02
  • 2021-11-22
  • 2021-10-12
相关资源
相似解决方案