शायद एक साधारण टाइपो त्रुटि ??
बस नीचे दिए गए
var currentUpload; // global
function uploadFile(localFullPath, headers, serverUrl)
{
var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath),
uploader = false;
try
{
uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
uploader.method = 'PUT';
// set headers to uploader
for (var key in headers)
{
if (headers.hasOwnProperty(key))
uploader.setRequestHeader(key, headers[key]);
}
}
catch (e)
{
// error handling
return false;
}
/*
return something it's a good practice if you want to chain Promises
|
|
V */
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri)
.then(
function success(file){
return uploader.createUpload(serverUrl, file);
},
function failure(error){
return WinJS.Promise.wrapError('file not found');
// |_____________|
// your are rejecting your promise with ---------^
}
).then(
function (uploadOperation){
// Just avoid this type of code construction
// and add brace !
// adding brace augment readability and prevent errors !
// where do you set this ??? currentUpload = 'Canceled'
// is part of winjs ???
if (currentUpload == 'Canceled')
// why not handle your rejected promise ?
// by something like :
//if(uploadOperation == 'file not found')
return WinJS.Promise.wrapError('upload canceled');
else
return (currentUpload = uploadOperation.startAsync());
}
).then(
// Promise resolve handler
function success(success){
currentUpload = false;
// success handling
return true;
} ,
// Promise reject handler
function failure(error){
currentUpload = false;
// error handling
return false;
}
/* ^
|
YOU HAVE HERE A DOUBLE CLOSING BRACE }} ???
°
| WHAT IS THIS PART FOR ??????
|
+------+---- Ending uploadFile with successive closing brace !
|
|+---------------- Declaration separator
|| +--- New function declaration
|| |
VV V */
}, function pending(status){
var progress = status.progress,
percent = Math.round(progress.bytesSent/progress.totalBytesToSend * 100);
// progress handling
});
}
/*
ALL THAT PART ABOVE IS NOT IN THE PROMISE CHAIN
AND SHOULD BREAK YOUR CODE !!!
HOW HAVE YOU EVER COULD RUN THIS ???
*/
कोड रों में टिप्पणियां देखनी शायद यह अपने कोड का एक हिस्सा याद आती है, क्योंकि इस राज्य में अपने कोड को तोड़ने चाहिए, इस बात का यकीन है कि हो सकता है!
हो सकता है कि एक adjustement हो सकता है:
var currentUpload; // global
function uploadFile(localFullPath, headers, serverUrl)
{
var fileUri = new Windows.Foundation.Uri('ms-appdata:///local' + localFullPath),
uploader = false;
try
{
uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
uploader.method = 'PUT';
// set headers to uploader
for (var key in headers)
{
if (headers.hasOwnProperty(key))
uploader.setRequestHeader(key, headers[key]);
}
}
catch (e)
{
// error handling
return false;
}
/*
return something it's a good practice if you want to chain Promises
|
|
V */
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileUri)
.then(
function success(file){
return uploader.createUpload(serverUrl, file);
},
function failure(error){
return WinJS.Promise.wrapError('file not found');// O|--------+
}// |
).then(// |
function (uploadOperation){// O|-------------------------------|
// |
// Just avoid this type of code construction |
// and add brace ! |
// adding brace augment readability and prevent errors ! |
// |
// EDIT |
//if (currentUpload == 'Canceled') { // <--- add brace |
if (uploadOperation == 'file not found') { //<---add brace <--+
return WinJS.Promise.wrapError('upload canceled');
} else { // <---- add braces
// even if it is valid
// avoid assignement like that
// so subtil to read/understand/debug ...
// return (currentUpload = uploadOperation.startAsync());
currentUpload = uploadOperation.startAsync();
// maybe debug here ?
console.log('currentUpload : ' + currentUpload);
return currentUpload;
} // <---- add brace
}
).then(
function success(success){
currentUpload = false;
// success handling
return true;
} ,
function failure(error){
currentUpload = false;
// error handling
return false;
} ,
function pending(status){
var progress = status.progress,
percent = Math.round(progress.bytesSent/progress.totalBytesToSend * 100);
// progress handling
}
).done(// it is always a good thing to have a final catcher !!
function(){
// things are ok !
console.log('ok');
},
function(err){
// make something with your error better than
alert(err);
console.log('ko');
}
);
}
संपादित
खोज रहे हैं आगे (i winjs उपयोग न करें), वहाँ कुछ स्पष्ट किया जा सकता है:
.then(
function success(file) {
return uploader.createUpload(serverUrl, file);
},
function failure(error) {
return WinJS.Promise.wrapError('file not found');
}
).then(
function(uploadOperation) {
if (currentUpload == 'Canceled')
return WinJS.Promise.wrapError('upload canceled');
else
return (currentUpload = uploadOperation.startAsync());
}
).
आप इसे कहां सेट कर रहे हैं:
currentUpload = 'Canceled'
आप इसे जांच रहे हैं, लेकिन मुझे यकीन है कि कोड का यह हिस्सा कभी नहीं पहुंचा है।
कि इससे पहले कि आपके साथ अपने वादा अस्वीकार:
return WinJS.Promise.wrapError('file not found');
शायद अगली then
उस तरह someting संभाल चाहिए:
.then(
function success(file) {
return uploader.createUpload(serverUrl, file);
},
function failure(error) {
return WinJS.Promise.wrapError('file not found');
}
).then(
function(uploadOperation) {
if (uploadOperation == 'file not found')
return WinJS.Promise.wrapError('upload canceled');
else
return (currentUpload = uploadOperation.startAsync());
}
)
मुझे आशा है कि यह आप में मदद मिलेगी।
मैं स्पष्ट रूप से समझ नहीं पा रहा हूं कि आप 'वापसी (currentUpload = uploadOperation.startAsync()) के साथ क्या हासिल करने का प्रयास कर रहे हैं; '। इसके अलावा आप गड़बड़ कर चुके हैं आप चेनिंग का वादा करते हैं। जब वादे का सामना करना पड़ता है, तो मुझे अंत में और उन सभी के लिए त्रुटि प्रबंधन करना चाहिए। यह वादा करने की वादा की सुंदरता है। – sebagomez
यह कथन वर्तमान अपलोड को वैश्विक चर में सहेजता है। कल्पना करें कि यह अपलोड वापस आएगाऑपरेशन.स्टार्टएसिंक() - इसका परीक्षण किया गया, त्रुटि तब भी हुई। "गड़बड़" के साथ आपका क्या मतलब है? क्या आप सर्वोत्तम अभ्यास और शैली के बारे में बात कर रहे हैं या इससे कोई त्रुटि हो सकती है? कारण मैंने ऐसा किया कि उसने मुझे केवल "WinRT त्रुटि" दी, इसलिए मैं त्रुटियों के बीच उचित रूप से अंतर नहीं कर सका (एक पाठ है लेकिन यह एक विशिष्ट भाषा के लिए बाध्य है)। – kerosene
[एमएसडीएन दस्तावेज़ीकरण] के अनुसार (https://msdn.microsoft.com/en-us/library/windows/apps/windows.networking.backgroundtransfer.downloadoperation.startasync.aspx), वह फ़ंक्शन प्रदर्शन को घटा सकता है। शायद आप मंदी का सामना कर रहे हैं। वे अनुशंसा करते हैं कि "आप पृष्ठभूमि कार्यकर्ता थ्रेड पर StartAsync को कॉल करें ..." क्या यह आपकी समस्या का स्रोत हो सकता है? – Jimmy