I want to upload a file using 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');
});
});
and in py file:
@view_config(route_name='ajax_post_file', renderer='json')
def live_search(request):
post_data = request.POST
but the request.Post says:
NoVars: Not an HTML form submission (Content-Type: text/plain)
Try changing to this in Ajax call
data: formData,