What should I return in response to show error mes

2019-07-20 16:30发布

I am using jQuery File Upload jQuery UI Plugin 8.7.2 from https://github.com/blueimp/jQuery-File-Upload Uploading and deleting of files work successfully. But what should I return in response JSON to show error when deleting of file not finished correctly on server side. For example user have not access for this.

This is my PHP code:

$response = json_encode(
    (object)
    [ 'files' =>
        [
            $file->filename => true,
        ]
    ]
);
return $response;

2条回答
Rolldiameter
2楼-- · 2019-07-20 17:05

Heres how I do it in Laravel. You should change the loop to deal with all files received in the POST

    $json = array(
        'files' => array()
    );

    foreach( $request->files as $file ){

        $filename = $file->getClientOriginalName().".".$file->getClientOriginalExtension();

        $json['files'][] = array(
            'name' => $filename,
            'size' => $file->getSize(),
            'type' => $file->getMimeType(),
            'error' => "Your error message"
        );
    }

    // Return error
    return response($json); //Laravel: the array get converted to json. You could call json_encode and pass it to your response
查看更多
等我变得足够好
3楼-- · 2019-07-20 17:13

The UI plugin doesn't seem to do anything with the response so you'd need to modify the jquery.fileupload-ui.js code in order to do something with an error response.

查看更多
登录 后发表回答