我正在尝试添加将zip文件上传到服务器的功能。客户端是使用AngularJS构建的,而服务器端是C#ASP.NET,但是我无法使其正常工作。

我的服务器端代码如下所示:

[System.Web.Script.Services.ScriptService]
public class xyz : System.Web.Services.WebService
{
// Other WebMethods that work fine

    [WebMethod]
    public string UploadFile(byte[] file, string filename)
    {
        // do whatever
    }
}


我正在使用ng-file-upload尝试进行实际的上传。 HTML看起来像:

<form>
    <div class="form-group">
        <label class="control-label h3" for="zipFile">Zip File</label>
        <input class="form-control my-file" type="file" ngf-select ng-model="zipFile" id="zipFile" name="zipFile" accept=".zip" required />
    </div>
    <button class="btn btn-default" ng-click="submit()">Submit</button>
</form>


我的控制器中的Javascript如下所示:

FileLoadController.$inject = ['$scope', 'Upload'];
function FileLoadController($scope, Upload) {
    var vm = this;
    vm.scope = $scope;
    vm.scope.submit = function () {
        if (vm.scope.zipFile) {
            vm.scope.upload(vm.scope.zipFile);
        }
    }

    vm.scope.upload = function (file) {
        Upload.upload({
            url: 'xyz.asmx/UploadFile',
            data: { 'file': file, 'filename': file.name },
            },
        })
        .then(function (response) {
            alert(response);
        });
    }
}


Upload.upload被调用,但是服务器端的UploadFile方法从不执行。

这可能很简单,但是我在做什么错呢?有没有更好或更简单的方法可以做到这一点?

谢谢。

最佳答案

我决定不尝试使用现有的WebHandler,而是专门为上载添加了IHttpHandler。我不想添加其他处理程序,但是不管用什么。

ng-upload-file(ng-file-upload .NET example)引用了一个示例,但让我总结一下我所做的。


在我的项目中添加了一个“通用处理程序”,并将其命名为UploadHandler。
调整ProcessRequest方法以执行我想要的操作
更新了AngularJS控制器以发送到新的处理程序
更新了HTML以允许大文件
调整web.config文件以允许上传大文件


(步骤1和2)将通用处理程序添加到项目中,将创建一个从IHttpHandler派生的类,它看起来像这样

public class UploadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];
                string fname = context.Server.MapPath("uploads\\" + file.FileName);
                file.SaveAs(fname);

                // Do something with the file if you want
            }
            context.Response.Write("File/s uploaded successfully");
        }
        else
            context.Response.Write("No files uploaded");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}


(步骤3)更新AngularJS控制器使其看起来像这样

    vm.scope.submit = function () {
        if (vm.scope.zipFile) {
            vm.scope.upload(vm.scope.zipFile);
        }
    }

    vm.scope.upload = function (file) {
        Upload.upload({
            url: 'UploadHandler.ashx',
            data: { },
            file: file
        })
        .then(function (response) {
            alert(response.data);
        })
    }


(第4步)添加了ngf-max-size,因此可以上传最大1GB的文件

<form>
    <div class="form-group">
        <label class="control-label h3" for="zipFile">Zip File</label>
        <input class="form-control my-file" type="file" ngf-select ng-model="zipFile" id="zipFile" name="zipFile" ngf-max-size="1GB" accept=".zip" required />
    </div>
    <button class="btn btn-default" ng-click="submit()">Submit</button>
</form>


(第5步)然后,我不得不调整web.config文件以允许那些大文件。它涉及添加两件事。首先是将maxRequestLength添加到httpRuntime中,如下所示:

<configuration>
  <!--Lots of omitted stuff-->
  <system.web>
    <httpRuntime targetFramework="4.5.1" maxRequestLength="1073741824" />
  </system.web>
</configuration>


第二个是添加一个安全部分,这样大的东西就不会被过滤掉:

<configuration>
  <!--Lots more omitted stuff-->
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824"/>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

10-06 13:26
查看更多