我正在使用来自tusdotnet的tus.io在ASP.NET核心应用程序中,我试图发送一个自定义的响应,因为我要把条目放在数据库中,并且需要在响应中返回数据库Id。在javascript端,在文件成功上传后,响应始终为null。
onSuccess: function (response) {
try {
console.log(response); // this part is always null. even if the json response was received in the final network call.
} catch (e) {
console.log('Error onSuccess: ', e);
}
}得把回应拿回来。
https://github.com/tus/tus-js-client/blob/master/docs/api.md
发布于 2022-06-01 07:47:28
结果,tusdotnet默认将响应状态代码发送回204 No Content,这意味着从库本身忽略了我的响应。
我必须将HttpContext.Response从Tus配置中更改为将状态代码发送为200 OK,以便将响应从asp.net core应用程序中传回。对于OnFileCompleteAsync事件中的exapmle,我必须将默认状态代码(204 No Content)更改为200 OK。
ctx.HttpContext.Response.StatusCode = StatusCodes.Status200OK;
await ctx.HttpContext.Response.WriteAsJsonAsync(new FileUploadResponseModel() { FileName = "Modified File Path", Path = "Extra info regarding the path", FileId = "Database Id" }, ctx.CancellationToken);为了在onSuccess回调中捕获响应:
onSuccess: function (response) {
try {
console.log(response); // This method should have the response after below changes
} catch (e) {
console.log('Error onSuccess: ', e);
}
}我必须修改javascript库,以便正确地emit响应,因为204 No Content的实现被修改为200 OK。负责处理此更改的函数是_emitSuccess。
function _emitSuccess() {
if (this.options.removeFingerprintOnSuccess) {
// Remove stored fingerprint and corresponding endpoint. This causes
// new uploads of the same file to be treated as a different file.
this._removeFromUrlStorage();
}
if (typeof this.options.onSuccess === 'function') {
this.options.onSuccess();
}
}必须修改上述方法以:
function _emitSuccess(res) {
if (this.options.removeFingerprintOnSuccess) {
// Remove stored fingerprint and corresponding endpoint. This causes
// new uploads of the same file to be treated as a different file.
this._removeFromUrlStorage();
}
if (typeof this.options.onSuccess === 'function') {
this.options.onSuccess(res);
}
}在进行此更改之后,我必须修改执行此回调的所有位置,以传递响应。例如在_handleUploadResponse回调中:
this._emitSuccess(res); // pass on the response received in the handler.同样,需要修改它正在消费的所有地方。
CDN:https://cdn.jsdelivr.net/npm/tus-js-client@latest/dist/tus.js
https://stackoverflow.com/questions/72458126
复制相似问题