【发布时间】:2020-04-25 01:36:19
【问题描述】:
在我的 extjs6 项目中,我将文件上传到我的 webapi。 (使用表单... fileuploadfield)文件被成功上传,它应该返回一个简单的字符串列表,但是即使文件被正确上传,在我的控制器中它总是在form.submit上返回FAILURE。原因...“阻止了来源为“http://localhost:57007”的框架访问跨域框架。”
我相信我在某处读到过,当我执行 form.submit 时,它会创建某种导致交叉原点的框架。
通常我不在乎它是否总是返回失败,因为工作仍在完成......但我想返回一些如果失败则无法工作的东西。有人可以帮助我以安全的方式执行此操作吗?
面板
xtype: 'form',
fileUpload: true, //(1)
width: 500,
frame: true,
title: 'Client Recap Upload Form',
bodyPadding: '10 10 10 10',
margin: '10px 10px 10px 10px',
standardSubmit: false,
defaults: {
anchor: '100%',
allowBlank: false,
msgTarget: 'side',
labelWidth: 50
},
items: [{
xtype: 'fileuploadfield',
emptyText: 'Select a file',
fieldLabel: 'Filename',
name: 'file',
buttonText: 'Choose a file'
}],
buttons: [
{
text: 'Upload',
listeners: {
click: 'onButtonFileUpload'
}
}
]
控制器
onUploadClientRecap: function (field, e, options, mid) {
var me = this;
if (field.up('form').getForm().isValid()) {
field.up('form').getForm().submit({
url: ExtApplication4.util.GlobalVar.urlTM_UploadClientRecap + mid,
waitMsg: 'Uploading your file...',
success: function (form, o)
{
Ext.Msg.show({
title: 'Result',
msg: o.response.responseText,//.result.result,
buttons: Ext.Msg.OK,
icon: Ext.Msg.INFO
});
},
failure: function (form, o)
{
debugger;
Ext.Msg.show({
title: 'Result',
msg: 'File Uploaded...',
buttons: Ext.Msg.OK,
icon: Ext.Msg.INFO
});
}
});
}
},
网页接口
[Route("api/tradematch/UploadClientRecap/{mid}")]
[HttpPost]
public List<string> UploadClientRecap(HttpRequestMessage request, int mid)
{
HttpContext context = HttpContext.Current;
HttpPostedFile postedFile = context.Request.Files["file"];
return _repo.UploadClientRecap(postedFile, mid);
}
在我的 webapi 中,我也在我的 application_beginrequest 中运行此代码
protected void Application_BeginRequest(object sender, EventArgs e)
{
string[] allowedOrigin = new string[5];
allowedOrigin[0] = "http://localhost:57007";
allowedOrigin[1] = "http://x.com";
allowedOrigin[2] = "https://x.com";
allowedOrigin[3] = "https://www.p.com";
allowedOrigin[4] = "http://www.p.com";
var origin = HttpContext.Current.Request.Headers["Origin"];
if (origin != null && allowedOrigin.Contains(origin))
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization, X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
尝试新的 webapi 返回重定向
[Route("api/tradematch/UploadClientRecap/{mid}")]
[HttpPost]
public HttpResponseMessage UploadClientRecap(HttpRequestMessage request, int mid)
{
HttpContext context = HttpContext.Current;
HttpPostedFile postedFile = context.Request.Files["file"];
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri("http://www.google.com/" + "&output=crudeOil");
return response;
//return _repo.UploadClientRecap(postedFile, mid);
}
【问题讨论】:
-
您将文件提交到哪里?一般的解决方案是通过发送适当的 http 标头来明确允许相关域之间的 CORS,但是从您的 sn-ps 无法给出任何具体建议...
-
会不会是您的前端运行在与您的 api 不同的端口上?您可以使用反向代理使它们在同一来源下可用 - 或者像 Mastacheata 之前所说的那样启用 CORS,更多信息在这里:developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
-
@Mastacheata 我刚刚更新了我的代码以向您展示 webapi。这是否为您提供了所需的信息?
-
@devbnz web api在本地1台pc上发布,前端在diff pc上发布,所以我确定它们是不同的。
-
我还添加了我在 Application_beginRequest 上运行的代码
标签: extjs cross-domain form-submit