【问题标题】:Save Canvas image with Deno使用 Deno 保存 Canvas 图像
【发布时间】:2021-02-09 10:45:21
【问题描述】:

我在客户端有一个 JS 脚本,它获取一些 Canvas,将其转换为 DataURL,并通过一些 FormData 发送。

     let image = canvas.toDataURL("image/png");
     let formData = new FormData();
     formData.append('Canvas_Image', image);

如何在 Deno(使用 Oak)中将其保存为文件图像?

【问题讨论】:

    标签: image canvas base64 deno


    【解决方案1】:

    问题现已解决,结果如下。

    首先,由于 toDataURL 方法将画布图像转换为 Base64 字符串,因此我使用了 Deno 的标准库作为编码/解码方法:

        import { decode as base64Decode, encode as base64Encode } from 'https://deno.land/std@0.82.0/encoding/base64.ts';
    
        // Read the FormData in Oak server:
        const body = await request.body({ type: 'form-data'});
        const form = await body.value.read();
    
         // Get the Image from the FormData (wich comes encoded in a base64 string)
         let Image = form.fields.Canvas_Image;
        
         // Replace characters so the decoding has no problems.
         Image = Image.replace("data:image/png;base64,", "");
         Image = Image.replace(" ", "+");
        
         // Use this method from the std library to decode it:
         let DecodedImage = base64Decode(Image);
        
         // Save the decoded image using Deno.writeFile()
         let URI_Image = "./path/to/save/your/image.png";
         await Deno.writeFile(URI_Image, DecodedImage);
    

    就是这样,它应该正确地将您的图像保存在服务器中。

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 2017-09-08
      • 2012-05-27
      • 2021-08-01
      • 2011-07-26
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多