本文介绍了是否FileReader.readAsBinaryString返回二进制或基于ASCII字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之,我希望将图片的二进制数据发送到我的处理程序,将被保存在数据库中的字节数组。读取使用一个文件输入的值检查可以更上什么正在通过HTTP发送线教你),并显示其作为或多或少可读的文本。

It depends on how you output it to your page, but the browser may or may not apply a certain encoding to the data it receives (a Fiddler inspection can teach you more on what's being sent over the HTTP wire) and display it as more or less readable text.

这并不适用于你的图片变量,虽然,这包含从<$c$c>readAsBinaryString()因此,在的形式原始二进制数据[原文]的。哦,松散类型的,谁在乎你回来的。我猜/希望的字节数组。现在,您需要发送给服务器。 preferably,上传的文件是由&LT处理;输入类型=文件/&GT; 元素,但有时你需要做的使用AJAX的事情。您中真正做到通过JavaScript真正的文件上传,虽然身为据我所知浏览器支持seems要增加。

This does not apply to your image variable though, which contains the actual, binary data from the readAsBinaryString() result, in the form of "raw binary data[sic]". Oh, loosely-typed, who cares what you return. I guess/hope a byte array. You now need to send this to a server. Preferably, file uploads are handled by <input type="file" /> elements, but sometimes you'll need to do things using AJAX. You can't really do real file uploads through JavaScript, although as far as I know browser support seems to be increasing.

所以,你需要 POST 与文件的内容张贴形式的参数之一。为了能够成功二进制数据一点,您需要正确的连接code将其表单提交。

So you'll need to POST with the file contents as one of the posted form's parameters. In order for this to happen successfully with binary data, you'll need to properly encode it for form submission.

首先确保该请求与应用程序/ x-WWW的形式urlen codeD的内容类型 。您可以在提琴手或咨询检查。后者没有提到任何编码在所有的,你必须figure说出来。

First make sure the request is made with the content-type of application/x-www-form-urlencoded. You can check this in Fiddler or by consulting the manual. The latter does not mention any encoding at all, you have to figure that out.

现在你需要url-en$c$c为了二进制数据将它张贴。该函数返回PTED为UTF-8的输入字节,除$ P $,因为这可以安全地发布了一个URL-CN codeD字符串。然后服务器端,您可以替换

Now you need to url-encode the binary data in order to post it. This function returns the input bytes, interpreted as UTF-8, as an URL-encoded string that can safely be posted. Then serverside, you can replace

System.Text.Encoding.GetEncoding(ISO-8859-1)GetBytes会(_Context.Request [形象]);

通过

System.Text.Encoding.UTF8.GetBytes(_Context.Request[\"Image\"]);

但是,为什么你有文件的内容以这种形式呢?由于提到,在的FileReader 还包含一个<$c$c>readAsDataURL方法,它允许您使用 reader.result 直接作为 POST 变量。

But why do you have the file's contents in that form anyway? As this answer mentions, the FileReader also contains a readAsDataURL method, which allows you to use reader.result directly as POST variable.

所以,你的code变成这样:

So, your code becomes like this:

var f = $("image").files[0];
var reader = new FileReader();
reader.readAsDataURL(f);
var image = reader.result;

$.ajax({
    url: theUrl,
    type: 'POST',
    data: { Image: image }
});

然后服务器端,你将不得不从基地64取消code中的数据:

Then serverside, you will have to decode the data from base 64:

byte[] imageBytes = System.Convert.FromBase64String(_Context.Request["Image"]);

如何运作的?

这篇关于是否FileReader.readAsBinaryString返回二进制或基于ASCII字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:55
查看更多