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