在使用xhr上传文件时,如何设置正确的编码 [英] Where and how do I set the correct encoding when uploading files using xhr

查看:420
本文介绍了在使用xhr上传文件时,如何设置正确的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上传图片文件,我需要他们使用重音字符,所以我已经把所有的东西都用在了$ code> UTF-8 。



我使用这个javascript函数上传它们:

  $('#files')。change function(e){
var formData = new FormData();
for(var i = 0; i< this.files.length; i ++){
formData.append(this.files [i] .name,this.files [i]);
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e){
if 4 == this.readyState){
showMessage(uploaded。);
}
};
xhr.open('post','accoms / upload.jsp' ,true);
xhr.send(formData);
},false);

所以如果我有一个名为Björk.jpg的文件,当我看看什么使用FireFox Firebug发送到服务器,显示为:

 内容处理:表单数据; NAME = Björk.jpg; filename =Björk.jpg

这似乎是服务器收到的。 p>

该帖子的编码显示为:

 内容类型文本/ html; charset = UTF-8 

当我以其他形式发送常规表单数据输入文本时发送,服务器正确接收到Björk一词。



这是服务器端代码,以防万一:

  mpp = new MultipartParser(request,100000000); 
com.oreilly.servlet.multipart.Part部分;
FilePart fp = null;
String fileName =;
files = new ArrayList();
while((part = mpp.readNextPart())!= null){
if(part.isFile()){
fp =(FilePart)part;
fileName = fp.getFileName();
文件文件=新建文件(fileName);
long size = fp.writeTo(file);
files.add(file);
}
}

任何想法?



谢谢。

解决方案

解析器使用自己的编码,从 API参考



尝试:

  mpp = new MultipartParser(request,100000000); 
mpp.setEncoding(UTF-8);
//其余的代码

引用建议在构造函数中传递: p>

  mpp = new MultipartParser(request,100000000,true,true,UTF-8); 


I am uploading image files, and I need them to work with accented characters - so I have made everything use UTF-8.

I use this javascript function to upload them:

     $('#files').change(function(e) {
            var formData = new FormData();
            for (var i=0; i<this.files.length;i++){
                formData.append(this.files[i].name, this.files[i]);
            }
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(e) {
                if ( 4 == this.readyState ) {
                    showMessage("Uploaded.");
                }
            };
            xhr.open('post', 'accoms/upload.jsp', true);
            xhr.send(formData);
        }, false);

So if I have a file named "Björk.jpg" for example, when I look at what gets sent to the server using FireFox Firebug it shows as:

Content-Disposition: form-data; name="Björk.jpg"; filename="Björk.jpg"

and that seems to be what the server receives.

The encoding for the post shows as this:

Content-Type    text/html;charset=UTF-8

When I send regular form data input text in other forms, it sends, and the server receives, the word "Björk" correctly.

Here is the server-side code in case its that:

mpp=new MultipartParser(request, 100000000);
com.oreilly.servlet.multipart.Part part;
FilePart fp=null;
String fileName="";
files=new ArrayList();
while((part=mpp.readNextPart())!=null){
    if (part.isFile()){
        fp=(FilePart)part;
        fileName=fp.getFileName();
        File file=new File(fileName);
        long size=fp.writeTo(file);
                    files.add(file);
            }
}

Any ideas?

Thanks.

解决方案

The parser uses its own encoding as is evident from the API reference.

Try:

mpp = new MultipartParser(request, 100000000);
mpp.setEncoding("UTF-8");
//rest of your code

The reference recommends passing it in constructor though:

mpp = new MultipartParser(request, 100000000, true, true, "UTF-8");

这篇关于在使用xhr上传文件时,如何设置正确的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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