代码之家  ›  专栏  ›  技术社区  ›  Enrico

SimpleMDE/EasyMDE用Blazor上传图片

  •  0
  • Enrico  · 技术社区  · 2 年前

    SimpleMDE EasyMDE 是Markdown的2个JavaScript编辑器。EasyMDE是SimpleMDE的扩展。我想在我的Blazor项目中使用EasyMDE。

    实现已经完成,您可以在上找到它 GitHub

    此编辑器具有上载图像或粘贴图像的功能。该物业 ImageUploadEndpoint 定义必须上传图像的位置。问题来了,我不会写ASP。NET核心控制器接收图像并保存。

    在EasyMDE的存储库中,有一个Python中的示例。首先,我必须创建编辑器

    我们需要使用imageUploadEndpoint选项指定发送图像的服务器地址:

    var easyMde = new EasyMDE({
        element: document.getElementById('md-text-area'),
        hideIcons: ['image'],
        showIcons: ['upload-image'],
        imageUploadEndpoint: "https://example.com/uploadImage"
    });
    

    然后,用Python编写脚本。

    @app.route('/imageUpload', methods=['POST'])
    def upload_image():
        """
        Upload an image to the server and store it to the folder defined in
        IMAGE_UPLOAD_FOLDER in flask configuration.
        Note that the image folder must be created first and with write access.
        :return: The relative path of the uploaded file, or an error among with the
        corresponding HTTP error code.
        """
        from werkzeug.utils import secure_filename
        import os.path as op
        if 'image' not in request.files:
            return 'no_file_part', 400  # Bad request
        file = request.files['image']
        if not file.filename:
            return 'no_selected_file', 400  # Bad request
        if file and '.' in file.filename \
                and file.filename.rsplit('.', 1)[1].lower() in 
                app.config['ALLOWED_IMAGES_TYPES']:
            file_name = secure_filename(file.filename)
            file_path = op.join(app.config['IMAGE_UPLOAD_FOLDER'], file_name)
            file.save(file_path)
            return file_path
        return 'no_allowed_file', 415  # Unsupported Media Type
    

    我不知道这个脚本是否有效。重点是如何在ASP中编写类似的api。NET核心?

    我写了这个API

    [ApiController]
    [Route("api/[controller]")]
    public class ImageController : Controller
    {
        [HttpPost]
        public async Task<IActionResult> Upload(IFormFile files)
        {
            long size = files.Length;
    
            var filePath = Path.GetTempFileName();
    
            using (var stream = System.IO.File.Create(filePath))
            {
                await files.CopyToAsync(stream);
            }
    
            return Ok();
        }
    }
    

    但是这个API没有接收到来自EasyMDE的任何请求。

    0 回复  |  直到 2 年前