I have an image which reader.readAsDataURL(file)
has encoded to base64 string. I have also used var imageBlob = ImageDataUrl.substring(ImageDataUrl.indexOf(",") + 1);
to strip out the data:image/png;base64
, part.
我有一个reader.readAsDataURL(文件)编码为base64字符串的图像。我还使用了var imageBlob = ImageDataUrl.substring(ImageDataUrl.indexOf(“,”)+ 1);去掉数据:image / png; base64,part。
now i have to send the base64 string via a json request using var link = "http://localhost:9002/AppAPI/rest/app/uploadImage?image="+imageBlob;
.
现在我必须使用var link =“http:// localhost:9002 / AppAPI / rest / app / uploadImage?image =”+ imageBlob;通过json请求发送base64字符串; 。
But postman receives no response and my guess is that some characters from the base64 string affects the Http request URL.
但邮递员没有收到任何回复,我的猜测是base64字符串中的某些字符会影响Http请求URL。
Please is there any way i can work around this?
请问有什么方法可以解决这个问题吗?
1 个解决方案
#1
0
GET is definitely not the right method to use when uploading an image. GETs must be both idempotent and safe (i.e. repeatable and read-only with no side effects). In fact, RFC 7231 4.2.1 explicitly warns against safe methods performing actions with side-effects based on query string parameters:
GET绝对不是上传图片时使用的正确方法。 GET必须是幂等的和安全的(即可重复和只读,没有副作用)。事实上,RFC 7231 4.2.1明确警告安全方法执行基于查询字符串参数的副作用操作:
For example, it is common for Web-based content editing software to use actions within query parameters, such as "page?do=delete". If the purpose of such a resource is to perform an unsafe action, then the resource owner MUST disable or disallow that action when it is accessed using a safe request method.
例如,基于Web的内容编辑软件通常在查询参数中使用动作,例如“page?do = delete”。如果此类资源的目的是执行不安全的操作,那么资源所有者必须在使用安全请求方法访问该操作时禁用或禁止该操作。
For an image upload, you probably want to do a PUT, with the image contents in the body of the request, not the query string (and therefore no need to base64 encode it). Placing the image contents in the query string is unlikely to work correctly. Most servers enforce a limit on URI length, which will almost certainly be exceeded by all but the smallest base64-encoded images. According to RFC 7230 3.1.1, the server must send a 414 response to the client when the URI is too long, though I wouldn't be surprised if there were non-compliant servers ignoring that requirement.
对于图像上传,您可能希望执行PUT,其中图像内容位于请求正文中,而不是查询字符串(因此无需对其进行base64编码)。将图像内容放在查询字符串中不太可能正常工作。大多数服务器对URI长度施加限制,除了最小的base64编码图像之外,几乎肯定会超过这个限制。根据RFC 7230 3.1.1,当URI太长时,服务器必须向客户端发送414响应,但如果有不合规的服务器忽略该要求,我不会感到惊讶。
You may need to escape the = sign(s) at the end of your base64-encoded string, but I recommend scrapping the base64-encoded query string parameter entirely.
您可能需要转义base64编码字符串末尾的=符号,但我建议完全删除base64编码的查询字符串参数。
#1
0
GET is definitely not the right method to use when uploading an image. GETs must be both idempotent and safe (i.e. repeatable and read-only with no side effects). In fact, RFC 7231 4.2.1 explicitly warns against safe methods performing actions with side-effects based on query string parameters:
GET绝对不是上传图片时使用的正确方法。 GET必须是幂等的和安全的(即可重复和只读,没有副作用)。事实上,RFC 7231 4.2.1明确警告安全方法执行基于查询字符串参数的副作用操作:
For example, it is common for Web-based content editing software to use actions within query parameters, such as "page?do=delete". If the purpose of such a resource is to perform an unsafe action, then the resource owner MUST disable or disallow that action when it is accessed using a safe request method.
例如,基于Web的内容编辑软件通常在查询参数中使用动作,例如“page?do = delete”。如果此类资源的目的是执行不安全的操作,那么资源所有者必须在使用安全请求方法访问该操作时禁用或禁止该操作。
For an image upload, you probably want to do a PUT, with the image contents in the body of the request, not the query string (and therefore no need to base64 encode it). Placing the image contents in the query string is unlikely to work correctly. Most servers enforce a limit on URI length, which will almost certainly be exceeded by all but the smallest base64-encoded images. According to RFC 7230 3.1.1, the server must send a 414 response to the client when the URI is too long, though I wouldn't be surprised if there were non-compliant servers ignoring that requirement.
对于图像上传,您可能希望执行PUT,其中图像内容位于请求正文中,而不是查询字符串(因此无需对其进行base64编码)。将图像内容放在查询字符串中不太可能正常工作。大多数服务器对URI长度施加限制,除了最小的base64编码图像之外,几乎肯定会超过这个限制。根据RFC 7230 3.1.1,当URI太长时,服务器必须向客户端发送414响应,但如果有不合规的服务器忽略该要求,我不会感到惊讶。
You may need to escape the = sign(s) at the end of your base64-encoded string, but I recommend scrapping the base64-encoded query string parameter entirely.
您可能需要转义base64编码字符串末尾的=符号,但我建议完全删除base64编码的查询字符串参数。