asp.net mvc - how to upload file with ng-file-upload in asp.mvc -
how upload file ng-file-upload in asp.mvc ?
i'm using code upload file ng-file-upload in asp.mvc :
public class homecontroller : controller, ihttphandler { public void processrequest(httpcontext context) { bool result; try { httppostedfile file = context.request.files[0]; if (file.contentlength > 0) { string namefile = guid.newguid().tostring("n") + path.getextension(file.filename); var path = path.combine(server.mappath(url.content("~/temp/")), namefile); file.saveas(path); } result = true; } catch (exception exception) { result = false; } context.response.write(result); } public bool isreusable { { return false; } } }
script :
$scope.$watch('file', function (file) { if (file&&!file.$error) { $scope.upload($scope.file); } }); $scope.upload = function (file) { upload.upload({ url: getbaseurl() + 'home/processrequest', file: file }).progress(function(evt) { $scope.progresspercentage = parseint(100.0 * evt.loaded / evt.total); console.log('progress: ' + $scope.progresspercentage + '% ' + evt.config.file.name); }).success(function (data, status, headers, config) { console.log('file ' + config.file.name + 'uploaded. response: ' + data); }).error(function (data, status, headers, config) { console.log('error status: ' + status); }); };
error :
post http://localhost:1726/home/processrequest 500 (internal server error) no parameterless constructor defined object.
edit :
i'm using code :
public class homecontroller : controller { [httppost] [allowuploadspecialfilesonly(".pdf,.doc,.docx,ppt,pptx,.mp3")] public jsonresult resume(httppostedfilebase file) { bool result; try { if (file != null && file.contentlength > 0 && file.filename != null) { string namefile = guid.newguid().tostring("n") + path.getextension(file.filename); var path = path.combine(server.mappath(url.content("~/temp/")), namefile); file.saveas(path); } result = true; } catch (exception exception) { result = false; } return json(new { result = result }, jsonrequestbehavior.allowget); } }
html :
<form class="form-horizontal text-center" role="form" name="upload_form" > <div class="form-group"> <div class="btn btn-primary" ngf-pattern="'.pdf,.doc,.docx,ppt,pptx,.mp3,.apk'" ngf-select ng-model="file">send</div> </div> <span class="progress" ng-show="file.progress >= 0"> <div class="ng-binding" style="width:{{file.progress}}%" ng-bind="file.progress + '%'"></div> </span> </form>
scropt :
$scope.upload = function (file) { upload.upload({ url: getbaseurl() + 'home/resume', file: file }).progress(function(evt) { $scope.progresspercentage = parseint(100.0 * evt.loaded / evt.total); console.log('progress: ' + $scope.progresspercentage + '% ' + evt.config.file.name); }).success(function (data, status, headers, config) { console.log('file ' + config.file.name + 'uploaded. response: ' + data); }).error(function (data, status, headers, config) { console.log('error status: ' + status); }); };
this fine. progresspercentage
100% file being uploaded
Comments
Post a Comment