ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 从php返回错误到dropzone.js

2019-08-24 07:39:36  阅读:286  来源: 互联网

标签:javascript php dropzone-js


我想从我处理上传的PHP代码中返回一个错误.

目前,如果php上传失败,JS仍然认为它成功了,我认为这与它返回的事实有关.

我试过返回false而不是字符串,但仍然运行this.on(‘success’)函数.

PHP

public function imageUpload(){
    //Сheck that we have a file

    if((!empty($_FILES["file"])) && ($_FILES['file']['error'] == 0)) {

      //Check if the file is JPEG image and it's size is less than 350Kb
      $filename = basename($_FILES['file']['name']);
      $ext = substr($filename, strrpos($filename, '.') + 1);

      if (($ext == "jpg") && ($_FILES["file"]["type"] == "image/jpeg") && 
        ($_FILES["file"]["size"] < 3500000)) {

        //Determine the path to which we want to save this file
          $newname = '/home/anglicvw/public_html/newdev/app/templates/default/images/testimages/'.$filename;
          //Check if the file with the same name is already exists on the server

          if (!file_exists($newname)) {

            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['file']['tmp_name'],$newname))) {
               echo "It's done! The file has been saved as: ".$newname;
            } else {
               echo "Error: A problem occurred during file upload!";
            }
          } else {
             echo "Error: File ".$_FILES["file"]["name"]." already exists";
          }
      } else {
         echo "Error: Only .jpg images under 350Kb are accepted for upload";
      }
    } else {
     echo "Error: No file uploaded";
    }
}

JS

$(document).ready(function(){

            Dropzone.autoDiscover = false;

            var myDropzone = new Dropzone('div#imagesdropzone', { url: '/admin/imageUpload',
            parallelUploads: 100,
            maxFiles: 100,});
        });

        Dropzone.options.imagesdropzone = {
            init: function() {
                this.on('success', function( file, resp ){
                  console.log( file );
                  console.log( resp );
                });
                this.on('error', function( e ){
                  console.log('erors and stuff');
                  console.log( e );
                });
            }
        };

解决方法:

是的这是对的.您必须设置HTTP标头.
看一下:

https://github.com/enyo/dropzone/wiki/FAQ#how-to-show-an-error-returned-by-the-server

如果您有HTTP错误并希望在DropzoneJS的悬停错误消息中显示它,您可以:

myDropzone.on("error", function(file, message, xhr) {
    var header = xhr.status+": "+xhr.statusText;
    $(file.previewElement).find('.dz-error-message').text(header);
  });

(你需要jQuery代码[$().find();])

另一种方法是通过PHP返回JSON错误消息:

//define text for error message
$output['error'] = 'No Token';

//return right HTTP code
if( $error ){
    http_response_code (401);
}
else{
    http_response_code (200);
}

//set Content-Type to JSON
header( 'Content-Type: application/json; charset=utf-8' );
//echo error message as JSON
echo json_encode( $output );

标签:javascript,php,dropzone-js
来源: https://codeday.me/bug/20190824/1705733.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有