简体   繁体   English

如何从jQuery上传文件到php服务器?

[英]How to upload files from jQuery to a php server?

i want to send the values of the files array to my PHP server, i try to do that but jquery returns undefined, offcourse i still new to jquery and here the code : 我想将文件数组的值发送到我的PHP服务器,我尝试这样做,但是jquery返回undefined,当然,我仍然对jquery还是这里的代码:

for ($i=1;$i<=$stars_settings['attachments']['max_number'];$i++)
{
    $cls = ($i == 1 && in_array('attachments',$_SESSION['iserror'])) ? ' class="isError" ' : '';
    echo '<input id="attachment['.$i.']" type="file" name="attachment['.$i.']" size="50" '.$cls.' /><br />';
}

how can i submit this id="attachment['.$i.']" ? 我如何提交此id="attachment['.$i.']"

You cannot upload files with just plain ol' jQuery. 不能仅使用普通的jQuery上传文件。

You need to be smart about it, either create your own methods to handle the uploads, or use a library/plugin that someone else created. 您需要对此有所了解,或者创建自己的方法来处理上传,或者使用其他人创建的库/插件。


For example you can do: 例如,您可以执行以下操作:

// Originally solved by Tim Branyen in his drop file plugin
// http://dev.aboutnerd.com/jQuery.dropFile/jquery.dropFile.js
// Allows for datatransfer and file uploads
jQuery.event.props.push('dataTransfer');

Then when someone drops a file into a dive from their desktop, you can do: 然后,当有人将文件从其桌面拖放到潜水中时,您可以执行以下操作:

$(document).on('drop', '.attachments', function(event) {
    var dt, files;
    event.preventDefault();
    event.stopPropagation();
    attachments.removeClass('hover');
    dt = event.dataTransfer;
    files = dt.files;
    // Do something with the raw files
});

Here is an example of a function you can use to handle raw files: 这是可用于处理原始文件的函数示例:

function handleFile(file) {
    var reader = new FileReader();
    reader.onload = function(event) {
       var dataURL = event.target.result;
       //upload the file via ajax
    };
    reader.readAsDataURL(file);
}

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

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