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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c