ASP Net Core Filepond 从服务器加载文件
ASP Net Core Filepond load file from server
我有一个项目,它使用 Filepond 上传文件,我需要它从服务器加载文件。
我已经关注 docs 但它不起作用。 Filepond 给出错误 Error during load 400
,它甚至不发送从服务器加载文件的请求
这是我的javascript
let pond = FilePond.create(value, {
files: [
{
// the server file reference
source: 'e958818e-92de-4953-960a-d8157467b766',
// set type to local to indicate an already uploaded file
options: {
type: 'local'
}
}
]
});
FilePond.setOptions({
labelFileProcessingError: (error) => {
return error.body;
},
server: {
headers: {
'@tokenSet.HeaderName' : '@tokenSet.RequestToken'
},
url: window.location.origin,
process: (fieldName, file, metadata, load, error, progress, abort) => {
// We ignore the metadata property and only send the file
fieldName = "File";
const formData = new FormData();
formData.append(fieldName, file, file.name);
const request = new XMLHttpRequest();
request.open('POST', '/UploadFileTemp/Process');
request.setRequestHeader('@tokenSet.HeaderName', '@tokenSet.RequestToken');
request.upload.onprogress = (e) => {
progress(e.lengthComputable, e.loaded, e.total);
};
request.onload = function () {
if (request.status >= 200 && request.status < 300) {
load(request.responseText);
}
else {
let errorMessageFromServer = request.responseText;
error('oh no');
}
};
request.send(formData);
},
revert: "/UploadFileTemp/revert/",
load: "/UploadFileTemp/load"
}
})
这是我的控制器
public async Task<IActionResult> Load(string p_fileId)
{
//Code to get the files
//Return the file
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");
return PhysicalFile(filePath, "text/plain");
}
NB
我已经通过邮递员测试了我的控制器并且它可以工作。我还检查了 content-disposition
header
我建议先设置所有选项,然后再设置文件 属性。
您正在设置 files
,然后告诉 FilePond 在哪里可以找到它们,它可能已经在尝试加载它们但还没有端点。
重构代码使其看起来像这样应该可以解决问题。
let pond = FilePond.create(value, {
server: {
headers: {
'@tokenSet.HeaderName': '@tokenSet.RequestToken',
},
url: window.location.origin,
process: (fieldName, file, metadata, load, error, progress, abort) => {
// your processing method
},
revert: '/UploadFileTemp/revert',
load: '/UploadFileTemp/load',
},
files: [
{
// the server file reference
source: 'e958818e-92de-4953-960a-d8157467b766',
// set type to local to indicate an already uploaded file
options: {
type: 'local',
},
},
],
});
我有一个项目,它使用 Filepond 上传文件,我需要它从服务器加载文件。
我已经关注 docs 但它不起作用。 Filepond 给出错误 Error during load 400
,它甚至不发送从服务器加载文件的请求
这是我的javascript
let pond = FilePond.create(value, {
files: [
{
// the server file reference
source: 'e958818e-92de-4953-960a-d8157467b766',
// set type to local to indicate an already uploaded file
options: {
type: 'local'
}
}
]
});
FilePond.setOptions({
labelFileProcessingError: (error) => {
return error.body;
},
server: {
headers: {
'@tokenSet.HeaderName' : '@tokenSet.RequestToken'
},
url: window.location.origin,
process: (fieldName, file, metadata, load, error, progress, abort) => {
// We ignore the metadata property and only send the file
fieldName = "File";
const formData = new FormData();
formData.append(fieldName, file, file.name);
const request = new XMLHttpRequest();
request.open('POST', '/UploadFileTemp/Process');
request.setRequestHeader('@tokenSet.HeaderName', '@tokenSet.RequestToken');
request.upload.onprogress = (e) => {
progress(e.lengthComputable, e.loaded, e.total);
};
request.onload = function () {
if (request.status >= 200 && request.status < 300) {
load(request.responseText);
}
else {
let errorMessageFromServer = request.responseText;
error('oh no');
}
};
request.send(formData);
},
revert: "/UploadFileTemp/revert/",
load: "/UploadFileTemp/load"
}
})
这是我的控制器
public async Task<IActionResult> Load(string p_fileId)
{
//Code to get the files
//Return the file
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("X-Content-Type-Options", "nosniff");
return PhysicalFile(filePath, "text/plain");
}
NB
我已经通过邮递员测试了我的控制器并且它可以工作。我还检查了 content-disposition
header
我建议先设置所有选项,然后再设置文件 属性。
您正在设置 files
,然后告诉 FilePond 在哪里可以找到它们,它可能已经在尝试加载它们但还没有端点。
重构代码使其看起来像这样应该可以解决问题。
let pond = FilePond.create(value, {
server: {
headers: {
'@tokenSet.HeaderName': '@tokenSet.RequestToken',
},
url: window.location.origin,
process: (fieldName, file, metadata, load, error, progress, abort) => {
// your processing method
},
revert: '/UploadFileTemp/revert',
load: '/UploadFileTemp/load',
},
files: [
{
// the server file reference
source: 'e958818e-92de-4953-960a-d8157467b766',
// set type to local to indicate an already uploaded file
options: {
type: 'local',
},
},
],
});