对于长标题感到抱歉。
所以我创建了一个角度应用程序,节点服务器作为我的后端。我之前已经上传了以下路线:
router.route('/resource')
.post(function (req, res) {
req.files.file.originalname = req.files.file.originalname.replace(/ +?/g, '_');
if (validate(req.body.type, req.files.file)) {
var path = createPath(req.body, req.user.user_id);
if (path) {
var media = new mediaModule.Media(req.files.file, path);
processData(media).then(function (result) {
res.json(result);
if (req.body.update == 'true' && req.body.oldResource) {
deleteFile(req.body.oldResource);
}
})
} else {
res.status(500).send('Incorrect path!');
}
} else {
res.status(500).send('Validation failed!');
}
})
上面的代码已经过测试,就像魅力一样。
现在你们知道你在Angular中创建了指令。我的一个指令是一个上传者,它看起来像这样:
/**
* Scope variables:
* lbUploadForm: {location: STRING(required), location_id: INTEGER (semi required), update: boolean, oldResource: STRING (if update = true then required) } (REQUIRED)
* uploadType: STRING (OPTIONAL)
* strictAccept: STRING (OPTIONAL)
* objectPointer: object (OPTIONAL)
* tokenField: STRING (OPTIONAL)
* layoutSize: STRING (REQUIRED)
* uploadCallback: function (OPTIONAL) (RECOMMENDED)
*/
angular.module('ResourceManager').directive('lbUploader', ['Query', 'FileUploader', 'ResourceFactory', function (Query, FileUploader, ResourceFactory) {
return {
restrict: 'E',
roles: 'admin',
templateUrl: getTemplate,
scope: {
lbUploadForm: '=',
uploadType: '@',
strictAccept: '@',
objectPointer: '=',
tokenField: '@',
layoutSize: '@',
uploadCallback: '='
},
link: function (scope, element, attr) {
scope.uploader = new FileUploader({
url: ResourceFactory.getUploadUrl(),
formData: [scope.lbUploadForm]
});
scope.footable = function (timeout) {
setTimeout(function () {
$('.footable').trigger('footable_resize');
$('.footable').trigger('footable_redraw');
}, timeout);
};
// CALLBACKS
scope.uploader.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {
console.info('onWhenAddingFileFailed', item, filter, options);
};
scope.uploader.onAfterAddingFile = function (fileItem) {
console.info('onAfterAddingFile', fileItem);
};
scope.uploader.onAfterAddingAll = function (addedFileItems) {
console.info('onAfterAddingAll', addedFileItems);
};
scope.uploader.onBeforeUploadItem = function (item) {
console.info('onBeforeUploadItem', item);
};
scope.uploader.onProgressItem = function (fileItem, progress) {
console.info('onProgressItem', fileItem, progress);
};
scope.uploader.onProgressAll = function (progress) {
console.info('onProgressAll', progress);
};
scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
console.info('onSuccessItem', fileItem, response, status, headers);
};
scope.uploader.onErrorItem = function (fileItem, response, status, headers) {
console.info('onErrorItem', fileItem, response, status, headers);
};
scope.uploader.onCancelItem = function (fileItem, response, status, headers) {
console.info('onCancelItem', fileItem, response, status, headers);
};
scope.uploader.onCompleteItem = function (fileItem, response, status, headers) {
if (status == 200) {
if (scope.uploadCallback != null) {
scope.uploadCallback(fileItem, response);
} else {
if (scope.tokenField && scope.objectPointer) {
scope.objectPointer[scope.tokenField] = response;
}
}
}
scope.uploader.onCompleteAll = function () {
console.info('onCompleteAll');
scope.uploader.clearQueue();
angular.element("input[type='file']").val(null);
};
}
}
};
function getTemplate(element, attr) {
var templateUrl = '';
var dirPath = 'js/helpers/ResourceManager/directives/lb-uploader/';
switch (attr.layoutSize) {
case 'xl':
case 'lg':
case 'md':
templateUrl = dirPath + 'lb-uploader-big.html';
break;
case 'sm':
case 'xs':
templateUrl = dirPath + 'lb-uploader-small.html';
break;
default:
templateUrl = dirPath + 'lb-uploader-big.html';
break;
}
return templateUrl;
}
}]);
现在上面也很好用,并且已经过测试并在我的应用程序中使用了多个位置。
问题
好的,现在我绕着我的惯常生意我在我的应用程序中有一个地方,我的用户想要上传文件
所以我添加了我的指令:
<lb-uploader ng-hide="CTRL.course.caption" strict-accept="image/*" lb-upload-form="CTRL.formData" layout-size="sm" upload-callback="CTRL.captionUploadCallback"></lb-uploader>
尝试上传。
但是这次发生了一些奇怪的事情并且会引导你完成它:
然后再次抛出它后,它再次尝试上传文件,这次成功
总共这个过程需要大约4-5分钟。
现在我试图在我的节点服务器中设置一个断点:
app.all('/', function (req, res, next) {
// CORS headers
res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
// Set custom headers for CORS
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key');
next();
});
然后我调试了服务器,但请求从未达到过这一点。
还有待处理的请求具有以下标题:
请注意,这是我整个应用程序中的唯一位置(相当大),它停止了,我在我的应用程序的所有位置使用相同的上传方法。
有没有人知道可能导致这种情况的原因?