पर काम किया है, तो कोड django view
पर फ़ाइल भेजने के लिए 10 का उपयोग कर रहा हूं। मुझे यह पता लगाने की आवश्यकता है कि फ़ाइल भेजी गई है या कुछ त्रुटि हुई है। मैंने jquery का उपयोग किया निम्नलिखित जावास्क्रिप्ट लिखें।अगर XMLHttpRequest.send() ने
आदर्श रूप में मैं उपयोगकर्ता को एक त्रुटि संदेश दिखाना चाहता हूं कि फ़ाइल अपलोड नहीं की गई थी। क्या javascript
में ऐसा करने का कोई तरीका है?
मैं, django view
से एक success/failure
संदेश लौटने success/failed message
json
के रूप में डाल और django view
इस .For से धारावाहिक json वापस भेजने के द्वारा ऐसा करने की कोशिश की, मैं xhr.open()
non-asynchronous
बनाया है। मैं मुद्रित करने के लिए xmlhttpRequest
वस्तु की responseText
व्याप्ति console.log(xhr.responseText)
से पता चलता
response= {"message": "success"}
मैं सोच रहा हूँ क्या है, कि क्या यह कई लेख this.In करने के लिए उचित तरीका है की कोशिश की, मैं चेतावनी में पाया गया कि
Async = false का उपयोग करने की अनुशंसा नहीं की जाती है
तो, क्या यह पता लगाने का कोई तरीका है कि फ़ाइल भेज दी गई है, जबकि xhr.open()
अतुल्यकालिक रखते हुए?
$(document).ready(function(){
$(document).on('change', '#fselect', function(e){
e.preventDefault();
sendFile();
});
});
function sendFile(){
var form = $('#fileform').get(0);
var formData = new FormData(form);
var file = $('#fselect').get(0).files[0];
var xhr = new XMLHttpRequest();
formData.append('myfile', file);
xhr.open('POST', 'uploadfile/', false);
xhr.send(formData);
console.log('response=',xhr.responseText);
}
मेरे django
दृश्य अर्क प्रपत्र डेटा से फाइल और एक गंतव्य फ़ोल्डर में लिखता है।
def store_uploaded_file(request):
message='failed'
to_return = {}
if (request.method == 'POST'):
if request.FILES.has_key('myfile'):
file = request.FILES['myfile']
with open('/uploadpath/%s' % file.name, 'wb+') as dest:
for chunk in file.chunks():
dest.write(chunk)
message="success"
to_return['message']= message
serialized = simplejson.dumps(to_return)
if store_message == "success":
return HttpResponse(serialized, mimetype="application/json")
else:
return HttpResponseServerError(serialized, mimetype="application/json")
संपादित करें:
मैं इस @ FabrícioMatté
xhr.onreadystatechange=function(){
if (xhr.readyState==4 && xhr.status==200){
console.log('xhr.readyState=',xhr.readyState);
console.log('xhr.status=',xhr.status);
console.log('response=',xhr.responseText);
var data = $.parseJSON(xhr.responseText);
var uploadResult = data['message']
console.log('uploadResult=',uploadResult);
if (uploadResult=='failure'){
console.log('failed to upload file');
displayError('failed to upload');
}else if (uploadResult=='success'){
console.log('successfully uploaded file');
}
}
}
'xhr.readyState == 4 && xhr.status == 200' पर्याप्त नहीं है? –
मेरी अज्ञानता को क्षमा करें, अगर django व्यू में फ़ाइल लेखन विफल रहता है और सर्वर त्रुटि होती है (500), क्या मैं इसका पता लगा सकता हूं कि 'xhr.readyState == 4 && xhr.status == 500' का उपयोग कर? – damon
प्रदान की गई आपकी 'xhr' ऑब्जेक्ट प्रतिक्रिया देता है, इसमें' स्थिति' और 'readyState' गुण भी होना चाहिए। सुनिश्चित करने के लिए 'console.log'ing' आज़माएं। =] –