C# 使用ASP.NETWebAPI和Ajax上传文件
我正在尝试使用ASP.NETWebAPI和Ajax上传一个图像文件。 我已将控制器中的Cors启用为C# 使用ASP.NETWebAPI和Ajax上传文件,c#,jquery,ajax,asp.net-web-api,C#,Jquery,Ajax,Asp.net Web Api,我正在尝试使用ASP.NETWebAPI和Ajax上传一个图像文件。 我已将控制器中的Cors启用为 [EnableCors("*", "*", "*")] public class myController: ApiController { 这也是上传图像的方法 [AllowAnonymous] [ActionName("PostUserImage")] public async Task<HttpResponseMessage>
[EnableCors("*", "*", "*")]
public class myController: ApiController
{
这也是上传图像的方法
[AllowAnonymous]
[ActionName("PostUserImage")]
public async Task<HttpResponseMessage> PostUserImage()
{
Dictionary<string, object> dict = new Dictionary<string, object>();
try
{
var httpRequest = HttpContext.Current.Request;
foreach (string file in httpRequest.Files)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
var postedFile = httpRequest.Files[file];
if (postedFile != null && postedFile.ContentLength > 0)
{
int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB
IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" };
var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
var extension = ext.ToLower();
if (!AllowedFileExtensions.Contains(extension))
{
var message = string.Format("Please Upload image of type .jpg,.gif,.png.");
dict.Add("error", message);
return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
}
else if (postedFile.ContentLength > MaxContentLength)
{
var message = string.Format("Please Upload a file upto 1 mb.");
dict.Add("error", message);
return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
}
else
{
var filePath = HttpContext.Current.Server.MapPath("~/Userimage/" + postedFile.FileName + extension);
postedFile.SaveAs(filePath);
}
}
var message1 = string.Format("Image Updated Successfully.");
return Request.CreateErrorResponse(HttpStatusCode.Created, message1); ;
}
var res = string.Format("Please Upload a image.");
dict.Add("error", res);
return Request.CreateResponse(HttpStatusCode.NotFound, dict);
}
catch (Exception ex)
{
var res = string.Format("some Message");
dict.Add("error", res);
return Request.CreateResponse(HttpStatusCode.NotFound, dict);
}
}
您是否将WebAPI应用程序与客户端应用程序分离?@Jeric Cruz:多页图像?这是什么?如果您的WebAPI托管在不同的域上,则需要启用CORS。有关如何启用CORS的更多信息,请查看此URL。重要的是,浏览器使用选项谓词发出prefly请求,请检查您的服务器上是否启用了此谓词。您是否将WebAPI应用程序与客户端应用程序分离?@Jeric Cruz:多页图像?这是什么?如果您的WebAPI托管在不同的域上,则需要启用CORS。有关如何启用CORS的更多信息,请查看此URL。重要的是,浏览器使用选项谓词发出prefly请求,请检查您的服务器上是否启用了此谓词。
<script type="text/javascript">
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
// Add the uploaded image content to the form data collection
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
// Make Ajax request with the contentType = false, and procesDate = false
var ajaxRequest = $.ajax({
type: "POST",
url: "https://link to my controller method",
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
// Do other operation
});
});
});
</script>
config.EnableCors();