我想使用ajax上传文件:
HTML:
<input type="file" name="photo" id="photo" />
<input type="button" value="Upload" id="btnUpload" />
avascript:
$(document).ready(function (){
var file_to_upload;
$('#photo').change(function() {
file_to_upload = this.files[0];
});
$('#btnUpload').click(function () {
var formData = new FormData();
formData.append('photo', file_to_upload);
$.ajax({
url: '/post_file',
type: 'POST',
data: {'photo' : formData},
cache: false,
contentType: false,
processData: false,
enctype: "multipart/form-data",
success: function () {
}
}, 'json');
});
});
并在py文件中:
@view_config(route_name='ajax_post_file', renderer='json')
def live_search(request):
post_data = request.POST
但是request.Post说:
NoVars:不是HTML表单提交(Content-Type:text / plain)
答案 0 :(得分:0)
我无法测试,但我确实看到了一些错误。试试这个装饰
@view_config(name='post_file', renderer='json')
答案 1 :(得分:0)
尝试在Ajax调用中更改为
data: formData,