简体   繁体   English

js上传文件-没有文件上传

[英]js upload file - No files were uploaded

I am uploading an image file on server-side and my script give an error 我正在服务器端上传图像文件,我的脚本给出了一个错误

error: "No files were uploaded." 错误:“没有文件上传”。

<form enctype="multipart/form-data" method="post">
  <input name="file" type="file"/>
  <input class="btn btn-warning" type="button" value="Upload"/>
</form>

JS code JS代码

$(':button').click(function () {
    var formData = new FormData($('form'));
    $.ajax({
        url: "/files/create",
        type: 'POST',
        success: completeHandler,
        data: formData,
        cache: false,
        contentType: false,
        processData: false

    });
    console.log(formData);
});

function completeHandler() {
    console.log("complete success");
}

Have anyone an idea? 有任何想法吗? With what it can be connected? 用什么可以连接?

Try this: 尝试这个:

//HTML Part

<form enctype="multipart/form-data" method="post">
    <input name="file" type="file"/>
    <input class="btn btn-warning" type="submit" value="Upload"/>
</form>

//Js part
<script>
$('form').submit(function (e) {
    e.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
       url: '/files/create',
       type: 'POST',
       data: formData,
       async: false,
       cache: false,
       contentType: false,
       enctype: 'multipart/form-data',
       processData: false,
       success: function (response) {
         alert(response);
       }
    });
    return false;
});

</script>

php part in your create.php create.php php部分

<?php
    if(isset($_FILES['file'])){
      print_r($_FILES);
    }
?>

暂无
暂无

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

相关问题 使用 Vue JS 和 Laravel 的文件上传表单 - 试图显示上传的文件列表? - File upload form with Vue JS & Laravel - trying to show list of uploaded files? Javascript,计算附加到文件上传输入的文件数量 - Javascript, count how many files were attached to a file upload input jQuery多个文件上传,保留尚未上传的文件 - jQuery Multiple file Upload, Retaining the files that have not been uploaded jQuery文件上传插件:上传所有文件时触发事件 - jQuery File Upload Plugin: trigger an event when all files are uploaded blueimp jQuery文件上传插件隐藏上传的文件 - blueimp jQuery file upload plugin hide uploaded files jQuery 文件上传 - 如何识别所有文件何时上传 - jQuery File Upload - how to recognise when all files have uploaded 使用vue axios和PHP上传多个文件 - 只上传一个文件 - Multiple files upload with vue axios and PHP - only one file is uploaded 尝试再次上传时如何使用ng-file-upload保留以前上传的文件? - How to retain visibly previous uploaded files using ng-file-upload when tried to upload again? 使用包含要上传的所有文件的路径的文本文件上传多个文件是否可行? - is it posible to upload multiple files using a text file that contains the path to all files to be uploaded? 反应.js。 当我尝试使用 FileReader 上传超过 3 个图像文件时,仅上传了 3 个 - React.js . when i am trying to upload more then 3 image files, using FileReader, only 3 uploaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM