不带表单的图像上传jquery

不带表单的图像上传jquery,jquery,Jquery,我有一个像这样的html <div id="cb"> <span> <br> "test.jpg" </span> <br> <img src="datat:image/jpeg;base64/9ikdsaasdflaljkf" class ="0"> </div> 但是这一行我没有得到任何价值。有人能帮我吗 试试这个: $(document).rea

我有一个像这样的html

<div id="cb">
    <span>
       <br>
       "test.jpg"
    </span>
    <br>
    <img src="datat:image/jpeg;base64/9ikdsaasdflaljkf" class ="0">
</div>
但是这一行我没有得到任何价值。有人能帮我吗

试试这个:

$(document).ready(function(){
    $('#upload').on('click', function() {
        var file_data = $('#pic').prop('files')[0];
        var form_data = new FormData();
        form_data.append('file', file_data);

        $.ajax({
                url         : 'upload.php',     // point to server-side PHP script 
                dataType    : 'text',           // what to expect back from the PHP script, if anything
                cache       : false,
                contentType : false,
                processData : false,
                data        : form_data,                         
                type        : 'post',
                success     : function(output){
                    alert(output);              // display response from the PHP script, if any
                }
         });
         $('#pic').val('');                     /* Clear the file container */
    });
});
</script>
</head>

<body>
<input id="pic" type="file" name="pic" />
<button id="upload">Upload</button>
</body>
upload.php

<?php
    if ( $_FILES['file']['error'] > 0 ){
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']))
        {
            echo "File Uploaded Successfully";
        }
    }

?>

文件控件最初没有文件,因此显示为空。 您可以在单击“上载”后提取文件,以便在浏览文件后在“上载”按钮单击事件中提取文件

$('#btn-upload').click(function(){

 var file = $("#cb")[0].files[0];
 alert(file); // Shows [object File] i.e. file object

 var fileName = $("#cb")[0].files[0].name; // Gives filename

});
你的代码是这样的

$'btn-upload'。单击函数{
var file=$cb[0]。文件[0]。名称; console.log文件; }; 上载
您可以使用Cloudinary的jQuery文件上载插件,通过此解决方案,您可以直接从浏览器上载图像

从GitHub项目的js文件夹下载jquery.cloudinary.js,并在HTML页面中,在包含jquery库之后包含jquery.cloudinary.js

<script src='jquery.min.js' type='text/javascript'></script>
<script src='jquery.cloudinary.js' type='text/javascript'></script>
并在HTML页面中包含所有必需的jQuery文件:

<script src='jquery.min.js' type='text/javascript'></script>
<script src='jquery.ui.widget.js' type='text/javascript'></script>
<script src='jquery.iframe-transport.js' type='text/javascript'></script>
<script src='jquery.fileupload.js' type='text/javascript'></script>
<script src='jquery.cloudinary.js' type='text/javascript'></script>

你可以在这里找到更多信息

var file=$cb[0]。文件[0]。名称;单击按钮“是”,这是不可能的?您想得到什么?我正在尝试上载文件“是的,我是这样做的:但在我的情况下,对于var file_data=$'pic'.prop'files'[0];我没有得到任何价值,很高兴它有帮助:-
jquery.ui.widget.js
jquery.iframe-transport.js
jquery.fileupload.js
<script src='jquery.min.js' type='text/javascript'></script>
<script src='jquery.ui.widget.js' type='text/javascript'></script>
<script src='jquery.iframe-transport.js' type='text/javascript'></script>
<script src='jquery.fileupload.js' type='text/javascript'></script>
<script src='jquery.cloudinary.js' type='text/javascript'></script>