我试图通过ajax发送一个文件到php文件,问题是当我试图发送数据时,我需要将其作为字符串(强制)发送,以以这种方式定位控制器和动作(函数):
jQuery.ajax({
type: "POST",
url: "/app/controllers/router.php",
data: {
controller: 'resource_management/language_control', action: "createLanguageControl", user: User, language: Language, course: Course, begin_date: BeginDate, end_date: EndDate, installments: Installments, monthly_amount: MonthlyAmount, max_reimbursement: MaxReimbursement, currency: Currency, TempDir : tempDir, DocName : docName
},
cache: false,
success: function (r) {
...
});
所以它经过路由器,通过字符串读取控制器和动作。我如何发送文件?
您可以尝试这样做:
var file = "someFile.csv";
var data = new FormData();
var controllerData = JSON.stringify({
controller: "resource_management/language_control",
action: "createLanguageControl",
.....
});
data.append("file", file);
data.append("string", controllerData);
$.ajax({
url: '/app/controllers/router.php',
method: "POST",
data: data,
contentType: false,
processData: false,
});