简体   繁体   English

如何使用angular将文件发送到asp.net Web API?

[英]How to send a file to asp.net web api with angular?

Basically what i want is to send a file to mi web api and then upload it to azure. 基本上我想要的是将文件发送到mi Web API,然后将其上传到Azure。 this code is triggered when i choose the file, but i'm not sure if it is the correct one. 当我选择文件时会触发此代码,但是我不确定它是否正确。

 $scope.fileNameChanged = function(files) { var formD = new FormData(); var reader = new FileReader(); reader.onload = function(e) { $scope.$apply(function() { $scope.files = reader.result; }); }; formD.append("file",files.files[0]); reader.readAsText(files.files[0]); $http({ method: 'POST', headers: {'Content-Type': undefined}, data:{formD}, transformRequest : angular.identity, url: 'http://localhost:12199/api/FacturacionWeb/PostFormData' }).success(function(data) { }).then(function(dat) { }) } 
 <input type="file" multiple onchange="angular.element(this).scope().fileNameChanged(this)"> <div>Import file...</div> <div> <textarea ng-model="files" accept=".txt" readonly="true" style="width:99%; height:500px" disabled></textarea> 

ASP.NET C# CODE (backend) this code is what i get in the backend but always get "UnsupportedMediaType" i dont know if the problem is with angular or with asp help me please!! ASP.NET C#代码(后端)此代码是我在后端得到的,但始终会得到“ UnsupportedMediaType”,我不知道问题是与角度还是与ASP有关,请帮帮我!!

   public async Task<HttpResponseMessage> PostFormData(HttpRequestMessage request)
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

Your angularjs code is fine. 您的angularjs代码很好。 You need to update your asp.net code like as- 您需要更新asp.net代码,例如as-

public async Task<HttpResponseMessage> PostFormData(HttpRequestMessage request)
    {
        // Check if the request contains multipart/form-data.
       if (!request.Content.IsMimeMultipartContent("form-data"))
            {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        try
        {
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                Trace.WriteLine("Server file path: " + file.LocalFileName);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

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

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