简体   繁体   English

如何在asp.mvc中使用ng-file-upload上传文件

[英]how to upload file with ng-file-upload in asp.mvc

how to upload file with ng-file-upload in asp.mvc ? 如何在asp.mvc中使用ng-file-upload上传文件?

I'm using this code to upload file with ng-file-upload in asp.mvc : 我正在使用此代码在asp.mvc中使用ng-file-upload上传文件:

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
        {
            get
            {
                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 for this object. POST http:// localhost:1726 / Home / ProcessRequest 500(内部服务器错误)没有为此对象定义无参数的构造函数。

edit : 编辑:

I'm using this 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 : 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 is fine. 这可以。 But progressPercentage is always 100% but the file is being uploaded 但是progressPercentage始终是100%,但是文件正在上传

here master https://github.com/danialfarid/ng-file-upload and see tutorial http://jsfiddle.net/danialfarid/0mz6ff9o/135/ 在这里掌握https://github.com/danialfarid/ng-file-upload并查看教程http://jsfiddle.net/danialfarid/0mz6ff9o/135/

STEP 0 follow the tutorial step above 步骤0, 按照上面的教程步骤

STEP 1 modif url parameter in index.cshtml STEP 1 modif url参数位于index.cshtml中

url: '@Url.Action("UploadDocument")', url:'@ Url.Action(“ UploadDocument”)',

file.upload = Upload.upload({
                  url: '@Url.Action("UploadDocument")',
                  data: {
                     File: file,
                     Description:'test'
                  }
               });

STEP 2 in c# Controller create UploadDocument(UploadRq data) c#控制器中的STEP 2创建UploadDocument(UploadRq数据)

 public JsonResult UploadDocument(UploadRq data)
      {

         //your code
         //string filename = Path.GetFileName(data.File.FileName);     
         //data.File.SaveAs(Server.MapPath("~/DocumentUploaded/") + filename);
         return Json("Saved", JsonRequestBehavior.AllowGet);
      }

STEP 3 create class UploadRq.cs STEP 3创建类UploadRq.cs

public class UploadRq
   {
      public HttpPostedFileBase File { get; set; }
      public string Description { get; set; }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM