angular项目中由于某些原因设置了以下代码:
// $locationProvider.html5Mode(true);
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function (obj) {
var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
for (name in obj) {
value = obj[name];
if (value instanceof Array) {
for (i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if (value instanceof Object) {
for (subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if (value !== undefined && value !== null)
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
return query.length ? query.substr(0, query.length - 1) : query;
};
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function (data) {
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}];
结果导致现在文件不能上传,最简单的一个form表单提交都不行:
<form action="upload/url" name="form1" method="post" enctype="multipart/form-data">
<input id="file" name="file" type="file" accept="image/*">
<button type="submit" class="btn btn-primary btn-lg">提交</button>
</form>
向各位大神求助,怎么样能正常上传图片?急,很急啊,项目都已经延期一天了……谢谢!
https://github.com/nervgh/ang... Prenez-le, de rien
Le sujet a posé deux questions : 1. Pourquoi les paramètres d'en-tête ont-ils été effectués 2. Comment télécharger des photos
?1. Pourquoi devez-vous définir l'en-tête ? Ajouter le paramètre d'en-tête au niveau de base httpProvider
http://stackoverflow.com/ques...
Les programmeurs d'entreprise devraient se référer à cette question. L'interaction API back-end de votre entreprise utilise Content-Type : x-www-form-urlencoded et angulaire utilise Content-type : application/json. Le type de contenu et la sérialisation ont donc été modifiés. Le sujet peut s'y référer.
2. Télécharger des photos
La description du sujet n'est pas très claire quant à la manière dont cette soumission a été initiée. Mais le problème vient du fait que le type de contenu du fichier soumis est mal défini. Fournissez un moyen de soumettre en utilisant FormData :
Votre code js consiste à encoder les données sous forme
x-www-form-urlencoded
. Mais il n’y a pas de liaison de données dans votre code HTML, ce qui n’est certainement pas possible ! Je soupçonne donc que vous n’avez pas du tout compris l’utilisation d’angularjs.De plus, vous n’avez pas expliqué clairement comment vous avez publié les données. Qui sait où se situe votre problème ?
Revenez après avoir posté le code complet