javascript sendfile二进制数据到Web服务 [英] javascript sendfile binary data to web service

查看:77
本文介绍了javascript sendfile二进制数据到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们正在尝试使用浏览器端的html 5/javascript和Web服务中的C#将文件从网页上传到Web服务.但是在进行某种编码时会遇到麻烦.

At work we are trying to upload files from a web page to a web service using html 5/javascript in the browser end and C# in the web service. But have some trouble with encoding of some sort.

对于javascript,我们在FileReader的帮助下获取文件的二进制数据.

As for the javascript we get the file's binary data with help from a FileReader.

var file = ... // gets the file from an input
var fileReader = new FileReader();
fileReader.onload = dataRecieved;
fileReader.readAsBinaryString(file);

function dataRecieved() {
  // Here we do a normal jquery ajax post with the file data (fileReader.result).
}

为什么我们要手动发布数据,而不是借助XmlHttpRequest(或类似工具)的帮助,是为了更轻松地从网页的不同部分(将其包装在函数中)整体发布到我们的Web服务.但这似乎不是问题.

Wy we are posting the data manually and not with help from XmlHttpRequest (or similar) is for easier overall posting to our web service from different parts of the web page (it's wrapped in a function). But that doesn't seem to be the problem.

Web服务中的代码如下所示

The code in the Web Service looks like this

[WebMethod]
public string SaveFileValueFieldValue(string value)
{
  System.Text.UnicodeEncoding encoder = new UnicodeEncoding();
  byte[] bytes = encoder.GetBytes(value);
  // Saves file from bytes here...
}

一切正常,数据似乎正常,但是当尝试打开文件(例如图像)时,无法将其打开.非常基本的文本文件似乎还可以.但是,如果我上载二进制"文件(如图片),然后在普通文本编辑器中以记事本的形式打开原始版本和上载版本,以查看有什么不同,那么似乎只有几个不可见"字符和某些东西是错误的从头开始显示几个字节作为新行.

All works well, and the data seems to be normal, but when trying to open a file (an image as example) it cannot be opened. Very basic text files seems to turn out okay. But if I upload a "binary" file like an image and then open both the original and the uploaded version in a normal text editor as notepad to see what differs, it seems to be wrong with only a few "invisible" characters and something that displays as a new line a few bytes in from from the start.

因此,从根本上说,该文件似乎在转换中的某处仅对几个字节进行了错误编码.

So basicly, the file seems to encode just a few bytes wrong somewhere in the conversions.

我也曾尝试用javascript从数据中创建一个int数组,然后再次将其转换为Web服务中的byte [],出现了完全相同的问题.如果我尝试使用unicode以外的任何其他格式(例如UTF-8)进行转换,则数据与原始数据完全不同,因此我认为此处的om正确,但是有些错误.

I've also tried to create an int array in javascript from the data, and then again transformed to a byte[] in the web service, with the exact same problem. If I try to convert with anything else than unicode (like UTF-8), the data turns out completly different from the original, so I think om on the right track here, but with something slightly wrong.

推荐答案

请求本身是文本,因此如果发送错误的enc类型,二进制数据将丢失. 您可以做的是将二进制编码为base64,然后在另一侧进行解码.
要将enc类型更改为多部分/混合并设置边界(就像电子邮件或其他内容一样),您必须自己组装请求.

The request itself is text, so binary data is lost if you send the wrong enc-type. What you can do is encode the binary to base64 and decode it on the other side.
To change the enc-type to multi-part/mixed and set boundaries (just like an e-mail or something) you'd have to assemble the request yourself.

这篇关于javascript sendfile二进制数据到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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