【问题标题】:Angular upload 405 (Method Not Allowed)角度上传 405(不允许的方法)
【发布时间】:2016-04-27 19:41:45
【问题描述】:

当尝试上传带有角度获取错误 405(不允许的方法)的图像时

Online example

我正在更改我的来源的标题,但又出现错误,有什么解决方案吗?

当将 URL 更改为 https://angular-file-upload-cors-srv.appspot.com/upload 时,在我的目的地无法正常工作!

【问题讨论】:

    标签: angularjs file-upload upload


    【解决方案1】:

    您的目标需要是处理 POST 的 Web API。 类似的东西:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web;
    using System.Web.Http;
    
    namespace WebAPI.Controllers
    {
    public class FileUploadController : ApiController
    {
    
        public HttpResponseMessage Post()
        {
            HttpResponseMessage result = null;
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.Count > 0)
            {
                var docfiles = new List<string>();
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    var filePath = HttpContext.Current.Server.MapPath("~/images/" + postedFile.FileName);
                    postedFile.SaveAs(filePath);
    
                    docfiles.Add(filePath);
                }
                result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            return result;
        }
    
    
    }
    }
    

    查看分步操作here。如需异步上传,请查看this

    MapPath 中的“~/images/”可以是虚拟文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 2023-03-23
      相关资源
      最近更新 更多