尝试使用角度获取错误405(方法不允许)上传图像时
我在源代码中更改了标题,但又出现了错误,任何解决方案?
当更改https://angular-file-upload-cors-srv.appspot.com/upload的网址时,我的目的网无法正常工作!
答案 0 :(得分:0)
您的目标需要是处理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;
}
}
}
&#34;〜/ images /&#34;在MapPath中可以是一个虚拟文件夹。