我创建了一个简单的webapi控制器。
// POST request api to get a string base64 image, store it and returns its name.
public string Post([FromBody]string image)
{
if (image == null)
return "No image sent";
// Generating random file name using the current date and time and random text
string fileName = "image-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + Path.ChangeExtension(
Path.GetRandomFileName(),
".jpg"
);
// If the directory does not exist create a new one
if (!Directory.Exists(@".\uploads\"))
{
DirectoryInfo DI = Directory.CreateDirectory(@".\uploads\");
}
File.WriteAllBytes(@".\uploads\" + fileName, Convert.FromBase64String(image));
return "Submitted as File: " + fileName;
}
当我通过邮递员发送邮件请求时,它工作正常并返回字符串"提交为文件..." 但是当我在Angular中尝试它时,我得到以下错误。
选项matplotlib documentation 405(方法不允许)
我的角色服务:
getResult(base64image) {
//Where base64image is a string.
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('http://example.com/Api/Image/Index', base64image, options)
.map(res => res);
}
我知道它与cors有关。但这只是一个简单的api。