C# MIME多部分流的预期结尾。MIME多部分消息不完整
我有一个Angular应用程序,用Typescript编写,带有ASP.NETWebAPI后端。我正在尝试使用ng file upload(有关详细信息,请参阅此)指令上载图像文件 我在Web API Post方法中收到异常: “MIME多部分流意外结束。MIME多部分消息未完成。” 我做过研究,也发现了类似的问题——我试图实现Landuber Kassa的答案,但没有成功 另外,尽管我的项目不是MVC,而且在任何情况下都不起作用 我是一个新的想法,并将感谢社会的想法。如果我能指出正确的方向,我很乐意考虑其他的选择。 灰烬 My.Net Post方法(实现Landuber Kassa的想法): 我的角度视图(指令的局部视图):C# MIME多部分流的预期结尾。MIME多部分消息不完整,c#,angularjs,asp.net-web-api,ng-file-upload,C#,Angularjs,Asp.net Web Api,Ng File Upload,我有一个Angular应用程序,用Typescript编写,带有ASP.NETWebAPI后端。我正在尝试使用ng file upload(有关详细信息,请参阅此)指令上载图像文件 我在Web API Post方法中收到异常: “MIME多部分流意外结束。MIME多部分消息未完成。” 我做过研究,也发现了类似的问题——我试图实现Landuber Kassa的答案,但没有成功 另外,尽管我的项目不是MVC,而且在任何情况下都不起作用 我是一个新的想法,并将感谢社会的想法。如果我能指出正确的方向,我
选择一个文件
上传
用C#试试这个:
[HttpPost]
[路线(“配置文件/图像”)]
公共任务上载imgprofile()
{
尝试
{
如果(!ModelState.IsValid)返回null;
var currentUser=\u userUtils.GetCurrentUser(用户);
if(currentUser==null)返回null;
HttpRequestMessage request=此.request;
如果(!request.Content.IsMimeMultipartContent())
抛出新的HttpResponseException(新的HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
string root=HttpContext.Current.Server.MapPath(“~”+Constant.Application.User\u Image\u目录);
bool exists=Directory.exists(根目录);
如果(!存在)
CreateDirectory(根目录);
var provider=新的MultipartFormDataStreamProvider(根);
var task=request.Content.ReadAsMultipartAsync(提供程序)。
继续(o=>
{
var finfo=新文件信息(provider.FileData.First().LocalFileName);
字符串guid=guid.NewGuid().ToString();
var fileName=guid+“”+currentUser.idown+”.jpg”;
Move(finfo.FullName,Path.Combine(root,fileName));
返回新的HttpResponseMessage()
{
Content=newstringcontent(Path.Combine(Constant.Application.User\u Image\u目录,文件名))
};
}
);
返回任务;
}
捕获(例外情况除外)
{
_logger.LogException(ex);
返回null;
}
}
角度控制器:
//Upload Func
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
$scope.uploading = true;
// $scope.imageName = file.name;
$upload.upload({
url: enviroment.apiUrl + '/api/CurrentUser/Profile/Image',
//fields: { 'username': $scope.username },
file: file
}).progress(function (evt) {
$scope.uploading = true;
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
$scope.progress = progressPercentage;
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
$scope.imageName = data;
$scope.uploading = false;
$scope.loadSuccess = true;
vm.uploadImage = false;
//AR
var reader = new FileReader();
reader.onload = function (evt) {
$scope.$apply(function ($scope) {
$scope.myImage = evt.currentTarget.result;
});
};
reader.readAsDataURL(files[0]);
//END AR
});
}
}
};
// Stay on Listen upload file
$scope.$watch('files', function (evt) {
$scope.upload($scope.files);
});
//上传函数
$scope.upload=函数(文件){
if(files&&files.length){
对于(var i=0;i
HTML:
加利卡伊马吉酒店
特拉西娜·奎拉·图阿·伊马吉,机会
苏拓电脑
将非支持文件拖放到da questo浏览器
public upload(): void {
//Create config used in ng-file-upload
var config: IFileUploadConfigFile = {
data: this.file, url: "BeaufortAppStore/api/Image/UploadImage/", method: "POST" };
this._dataService.uploadImage(config).then((result: any) => {
this.thumbnail = result.data;
});
}
<div class="form-group">
<label for="file" class="control-label col-xs-2">Choose a file</label>
<input id="file" type="file" name="file" class="form-control" ngf-select ngf-pattern="'image/*'"
ng-model="vm.file" />
<img style="width:100px;" ngf-thumbnail="thumbnail || '/thumb.jpg'" />
<button type="submit" ng-click="vm.upload()">Upload</button>
[HttpPost]
[Route("Profile/Image")]
public Task<HttpResponseMessage> UploadImgProfile()
{
try
{
if (!ModelState.IsValid) return null;
var currentUser = _userUtils.GetCurrentUser(User);
if (currentUser == null) return null;
HttpRequestMessage request = this.Request;
if (!request.Content.IsMimeMultipartContent())
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
string root = HttpContext.Current.Server.MapPath("~" + Constant.Application.User_Image_Directory);
bool exists = Directory.Exists(root);
if (!exists)
Directory.CreateDirectory(root);
var provider = new MultipartFormDataStreamProvider(root);
var task = request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(o =>
{
var finfo = new FileInfo(provider.FileData.First().LocalFileName);
string guid = Guid.NewGuid().ToString();
var fileName = guid + "_" + currentUser.IdOwin + ".jpg";
File.Move(finfo.FullName, Path.Combine(root, fileName));
return new HttpResponseMessage()
{
Content = new StringContent(Path.Combine(Constant.Application.User_Image_Directory, fileName))
};
}
);
return task;
}
catch (Exception ex)
{
_logger.LogException(ex);
return null;
}
}
//Upload Func
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
$scope.uploading = true;
// $scope.imageName = file.name;
$upload.upload({
url: enviroment.apiUrl + '/api/CurrentUser/Profile/Image',
//fields: { 'username': $scope.username },
file: file
}).progress(function (evt) {
$scope.uploading = true;
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
$scope.progress = progressPercentage;
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
$scope.imageName = data;
$scope.uploading = false;
$scope.loadSuccess = true;
vm.uploadImage = false;
//AR
var reader = new FileReader();
reader.onload = function (evt) {
$scope.$apply(function ($scope) {
$scope.myImage = evt.currentTarget.result;
});
};
reader.readAsDataURL(files[0]);
//END AR
});
}
}
};
// Stay on Listen upload file
$scope.$watch('files', function (evt) {
$scope.upload($scope.files);
});
<div class="row">
<!--UPLOAD-->
<div class="up-buttons">
<div class="clearfix visible-xs-block"></div>
<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12 text-center box-upload-image" data-ng-show="profileCtrl.uploadImage">
<br />
<div id="imgDragDrop" ng-file-drop ng-model="files"
drag-over-class="dragover"
accept="image/*">
<div class="cropArea-bkg">
<h4>
<span class="mdi mdi-account mdi-48px"></span>
<br /><br />
Carica immagine profilo
</h4>
<p>Trascina qui la tua immagine, oppure</p>
<div ng-file-select="" ng-model="files" class="btn btn-secondary" ng-accept="'*.pdf,*.jpg,*.png'" tabindex="0">
Sfoglia sul tuo computer
</div><br>
</div>
</div>
<div ng-no-file-drop class="well bg-danger">File Drag/Drop non è supportato da questo browser</div>
<br />
<div class="text-center">
<div class="progress" ng-show="uploading">
<div class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="{{ ::progress }}" aria-valuemin="0" aria-valuemax="100" style="width: {{::progress}}% ">
<span class="sr-only">{{ ::progress }}% Complete</span>
</div>
</div>
</div>
</div>
<!--END UPLOAD-->
</div>
</div>