最近在写一个tornado 与 angularJS 搭建的一个web项目,刚实现了一个图片上传功能,还比较简陋,简单的在这做个笔记。
//upload.html
<input type="file" name="file"
onchange="angular.element(this).scope().uploadFile(this.files)"/>
//upload.js
$scope.uploadFile = function (files) {
var fd = new FormData();
fd.append("file", files[0]);
$http.post("/api/upload", fd, {
withCredentials: true,
headers: {'Content-Type': undefined},
transformRequest: angular.identity
}).success(function (data) {
console.log("do something");
});
};
只贴了上传文件的部分代码,controller就不在此赘述了。
DIR = "/upload/media/"
class UploadHandle(tornado.web.RequestHandler):
def post(self, *args, **kwargs):
fileinfo = self.request.files["file"][0]
fname = fileinfo['filename']
cname = DIR + "test" +"."+fname.split(".")[-1]
fh = open(cname, 'w')
fh.write(fileinfo['body'])
self.finish("success")
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123