将base64转换为文件,然后以< form>< input name ='file'/>上传. [英] Convert base64 to file to upload in <form><input name='file'/>

查看:341
本文介绍了将base64转换为文件,然后以< form>< input name ='file'/>上传.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将base64解码为二进制

I'm decoding a base64 to binary

var image ="data:image/png;base64,iVBOR..."
var decoded = atob(image.split(",")[1])

然后要通过表单上传它:

Then want to upload it via a form:

<form action="https://storage.googleapis.com/YOUR_BUCKET_NAME" 
      method="post" enctype="multipart/form-data">
  <input name="key" type="text" value="objectName.txt" /><br/>
  <input type="hidden" name="file" />
  <input type="submit" value="Upload!" />
</form>

我将input的值和name="file"设置为decoded字符串,但是会导致invalid image.

I set the value of input with name="file" to the decoded string, but it will result in an invalid image.

我尝试将其转换为blob,但是当我将其设置为值时会导致[Object blob]

I tried converting it to a blob, but when I set that as the value it results in [Object blob]

我在这里想念什么?

当我使用: http://decodebase64.com/并输入base64字符串时,它会输出相同的数据,但是copy/pasting到文件中的数据显然不起作用.

When I use: http://decodebase64.com/ and input the base64 string, it outputs the same data, but copy/pasting that into a file, does not work apparently.

我肯定会错过解码数据并将其制作为工作文件之间的重要步骤...

I'm surely missing an essential step between decoding the data, and making it into a working file...

  • 我必须对字符串进行编码吗?如何编码?
  • 我如何找出应该被编码为什么?

(我首先浏览文件,与表单分开.然后我使用一些库进行裁剪,该库会吐出一个有效的base64字符串.现在,我只需要form即可与我一起工作并放入解码后的数据.但是就像我提到的,手动复制/粘贴也不起作用,所以缺少了一个步骤...)

(I'm browsing the file first, separately from the form. And then I crop it with some library, which spits out a working base64 string. Now I just need the form to work with me and put this decoded data inside it. But like I mentioned, copy/pasting manually also doesn't work, so there is a step missing...)

推荐答案

我尝试了同样的方法,结果发现您无法手动设置文件以表格形式上载.似乎是浏览器的安全性. 我的解决方法是包括第二个类型为'text'的隐藏输入字段,然后将Base64文本插入其中. 然后,服务器端检查是否设置了任何一个. 注意:现在,我继续搜索如何从输入的文件"中获取Base64;)

I have tried the very same thing and it turns out you can not manually set the file to upload in a form. It seems like a browser security thing. My workaround is to include a second hidden input field of type 'text' then pump the Base64 text into it. Then server side check if either is set. Note: Now I continue my search on how to get the Base64 out of the input 'file' ;)

这篇关于将base64转换为文件,然后以&lt; form&gt;&lt; input name ='file'/&gt;上传.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆