douzhongju8780 2015-04-08 15:38
浏览 19
已采纳

使用jQuery上传非拉丁文名称文件

I have simple form to ajax-upload files to server.

Here is input event listener:

$(document).on('change', '.js-file', function(event){

    sendFile(event);
});

And here is the function that actually uploads the file:

function sendFile (event) {
    var files = event.target.files;


    var action = '/ajax-upload-files/';
    var data = new FormData();

    $.each(files, function(key, value)
    {
        data.append(key, value);
    });

    $.ajax({
        url: action,
        type: 'POST',
        data: data,
        cache: false,
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function(data){

        },
        complete: function(data) {
        }
    });
}

It wors fine with all kinds of files, except those with non-latin characters in the file name, e.g. Максим.jpg

The data is being sent to server, as I see in Chrome network. But when I try to dump $_FILES on server, it seems to be empty.

var_dump($_FILES) //array(0) {}

I don't really understand what's wrong - contentLength of the request header is much smaller then it should be - it looks like file was not appended to form for some reason.

  • 写回答

2条回答 默认 最新

  • douqiao7188 2015-04-11 13:13
    关注

    I finally managed to upload non-latin name file. Have to read it as binary and send as binary.

    HTML:

    <input class="js-file" name="filename" type="file">
    

    And js

    //Handler in input
    $(document).on('change', '.js-file', function(event){
                var file_name = $(this).attr('name');
    
    
                var reader = new FileReader();
                var f = event.target.files[0],
                    nameArr = event.target.files[0]['name'].split('.'),
                    extension = nameArr[nameArr.length-1]; //here we can check if extension is allowed
    
                reader.onload = function(e) {
                    var contents = e.target.result;
                    contents = contents.split('base64,')[1]; //we need only encoded part
    
                    sendFileAsBinary(file_name, extension, contents);
                };
    
                reader.readAsDataURL(f);
    
    
            });
    

    And sender:

    function sendFileAsBinary(file_name, extension, img) {
    
        $.ajax({
            url: '/ajax-upload-files/',
            type: 'POST',
            data: {
                name: file_name,
                img: img,
                extension: extension
            },
            dataType: 'json',
            success: function(data){
    
    
            },
            complete: function(data) {
    
            }
    
        })
    
    }
    

    And php code to decode image:

     $name = $this->_getParam('name');
     $extension = $this->_getParam('extension');
     $computedName = $name . '.' . $extension;
    
     $fileContent = base64_decode($this->_getParam('img'));
    
     file_put_contents($fileContent, $computedName);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?