我用ckeditor 5记录产品描述
这是我的客户代码:
class MyUploadAdapter {
constructor(loader) {
this.loader = loader;
}
upload() {
return this.loader.file
.then(file => new Promise((resolve, reject) => {
this._initRequest();
this._initListeners(resolve, reject, file);
this._sendRequest(file);
}));
}
abort() {
if (this.xhr) {
this.xhr.abort();
}
}
_initRequest() {
const xhr = this.xhr = new XMLHttpRequest();
xhr.open('POST', '/Products/UploadFileDescription', true);
xhr.responseType = 'json';
}
_initListeners(resolve, reject, file) {
const xhr = this.xhr;
const loader = this.loader;
const genericErrorText = `Couldn't upload file: ${file.name}.`;
xhr.addEventListener('error', () => reject(genericErrorText));
xhr.addEventListener('abort', () => reject());
xhr.addEventListener('load', () => {
const response = xhr.response;
if (!response || response.error) {
return reject(response && response.error ? response.error.message : genericErrorText);
}
resolve({
default: response.Result
});
});
if (xhr.upload) {
xhr.upload.addEventListener('progress', evt => {
if (evt.lengthComputable) {
loader.uploadTotal = evt.total;
loader.uploaded = evt.loaded;
}
});
}
}
_sendRequest(file) {
const data = new FormData();
data.append('file', file);
this.xhr.send(data);
}
}
function MyCustomUploadAdapterPlugin(editor) {
editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
return new MyUploadAdapter(loader);
};
}
ClassicEditor
.create(document.querySelector('#editor'), {
extraPlugins: [MyCustomUploadAdapterPlugin],
})
.then(editor => {
window.editor = editor;
})
.catch(error => {
console.error(error);
});
这是我的服务器保存图像的端代码:
[HttpPost]
[Route("/Products/UploadFileDescription")]
public IActionResult UploadFileDescription(IFormFile file)
{
if (file != null)
{
if (file.Length > 0)
{
var fileName = NameGenerator.GenerateUniqCode() + Path.GetExtension(file.FileName);
string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/products/descriptionFiles", fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
return Json(new { url = filePath });
}
}
}
return new JsonResult("Error");
}
我的照片在我保存的路径下是正确保存的,但是照片的地址是错误的,所以照片没有显示在产品描述中。例如:
如有任何建议,我将不胜感激。