单文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-dataname="field1"
Joe Blow
--AaB03x
content-disposition: form-dataname="pics"filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
多文件:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-dataname="field1"
Joe Blow
--AaB03x
content-disposition: form-dataname="pics"
Content-type: multipart/mixed, boundary=BbC04y
--BbC04y
Content-disposition: attachmentfilename="file1.txt"
其次,python上传文件的几种方法:
1 自己封装皮祥困HTTP的POST数据包:http//stackoverflowcom/questions/680305/宴链using-multipartposthandler-to-post-form-data-with-python
import httplibimport mimetypesdef post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = httplib.HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() def encode_multipart_formdata(fields, files): LIMIT = '----------lImIt_of_THE_fIle_eW_$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + LIMIT) L.append('Content-Disposition: form-dataname="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files:
angular在1.5.5以上的版本中,在$http中也加入了eventHandler和uploadEventHandlers等方法,所以可以直接这样写:$http({
method: 'POST',
url: url,
eventHandlers: {
progress: function(c) {
console.log('Progress ->' + c)
console.log(c)
}
},
uploadEventHandlers: {
progress: function(e) {
console.log('UploadProgress ->' + e)
console.log(e)
}
},
data: uploadData,
}).success(function(data) {
console.log(data)
}).error(function(data, status) {
console.log(data)
})
最后,也可以用比较凳枝成熟举粗搜的组件去解决,正历推荐angular-file-upload
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)