【发布时间】:2014-12-08 21:37:18
【问题描述】:
我有一个客户端 HTML 应用程序和一个 Asp.net WebAPI 作为服务器应用程序。
我有一个场景,我必须提交表单,作为表单提交的一部分,我需要将表单数据发布到数据库。这对我有用,但是客户端应用程序如何知道在不同域中发生的数据库操作的成功或失败状态。我试图将HttpResponseMessage 对象返回给客户端,但是我的整个 HTML 页面都被重写为我从服务器返回的状态。
有什么方法可以单独检查特定状态,而不是使用从服务器 API 应用程序获得的响应重写整个 HTML,以便我在客户端应用程序中拥有更多控制权?
提交表单的客户端代码:
function ValidateFileAndSubmit() {
var myForm = $("#form1");
//UploadFiles();
var rootServicePath = config.rootURL;
var path = rootServicePath + 'api/Upload/UploadFile';
myForm.attr('action', path);
myForm.submit();
}
我在其中访问 POST 调用的 Web Api 代码:
[HttpPost]
public HttpResponseMessage UploadFile()
{
HttpResponseMessage response = null;
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
HttpContext.Current.Response.ContentType = "text/HTML";
var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
response = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent("Uploaded Successfully")
};
}
}
return response;
}
【问题讨论】:
-
你能发布 webAPI 和 html 的示例代码
-
我又加了一个答案,看看吧!
标签: c# jquery asp.net asp.net-web-api