简体   繁体   English

通过带有jQuery Easy ui的ajax上传文件

[英]upload file via ajax with jquery easy ui

I'm trying to give users the possibility to import data from a file that is located on their computer by using jQuery EasyUI form widget : 我正在尝试使用户能够使用jQuery EasyUI表单小部件从位于其计算机上的文件中导入数据:

<form id="my_form" method="POST">
    <input type="file" name="my_file" id="my_file" />
</form>

var file_name = $('#my_file').val();

if(file_name)
{       
    $('#my_form').form('submit', {
        url: [url_to_call],
        onSubmit: function(param){
             param.file_path = file_name;
        }
    });
}

Then when the user browse on his/her computer for the file, I wanna send the path to a jQuery ajax query to perform some upload action. 然后,当用户在他/她的计算机上浏览该文件时,我想将路径发送到jQuery ajax查询以执行一些上载操作。 The problem I have is that filename returns something like this: 我的问题是filename返回如下内容:

C:\fakepath\[name_of_file]

Because of the fakepath string, I am not getting the real path to where the file is located on the user's computer. 由于使用fakepath字符串,因此无法获取文件在用户计算机上的真实路径。 Does anybody know how I can fix this issue please? 有人知道我该如何解决这个问题?

Thank you 谢谢

Tried this plugin http://malsup.com/jquery/form/#getting-started 尝试了此插件http://malsup.com/jquery/form/#getting-started

this plugin provide a method called AjaxSubmit() which work fine for upload the image. 该插件提供了一种称为AjaxSubmit()的方法,可以很好地上传图像。 I have used it in 2011. 我在2011年使用过它。

it's have some issue in IE (old version like 6,7). IE(旧版本,例如6,7)中存在一些问题。 You need to write some hacks. 您需要编写一些技巧。 on the backend Firefox and IE upload the correct file-stream format like Data/Image but in Chrome (Webkit) you will got Octet Stream. 在后端Firefox和IE上载正确的文件流格式(如Data/Image但在Chrome(Webkit)中,您将获得八位字节流。 You need to parse the file format on server to check that file is not wrong. 您需要解析服务器上的文件格式,以检查文件是否正确。

I have no idea what your form() function does, as no such method exists in jQuery, but the usual way of uploading a file to the server would be something like this : 我不知道你的form()函数做什么,因为jQuery中没有这样的方法,但是将文件上传到服务器的通常方法是这样的:

$('#my_file').on('change', function() {
    $.ajax({
        url  : [url_to_call],
        data : new FormData($('#my_form').get(0)),
        processData: false,
        contenttype: false
    }).done(function(param) {
        param.file_path = file_name;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM