最近在写一个tornado 与 angularJS 搭建的一个web项目,刚实现了一个图片上传功能,还比较简陋,简单的在这做个笔记。
//upload.html
//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\")