【问题标题】:Unprocessable Entity when trying to upload file via Ajax尝试通过 Ajax 上传文件时无法处理的实体
【发布时间】:2019-01-24 06:04:57
【问题描述】:

我刚刚将我的本地代码移至实时服务器。一切正常,但是当我尝试在实时服务器上上传图像时,出现“无法处理的实体”错误。文件上传在我的本地服务器上完美运行。我正在使用带有 IIS 和 PHP 7.2 的 Windows 服务器,我的项目正在使用 laravel 5.5

我已经尝试向 IUSER 和 IIS_IUSRS 授予完全控制权限。我还尝试更新 php.ini 文件(file_uploads = On,upload_max_filesize = 20M,post_max_size = 20M)

我的表格

<form class='form-inline' id='edit-property-image' enctype='multipart/form-data'>
    <input type='hidden' id='property' value='$id' name='property'>
    <input type='file' class='custom-file-input' id='propertyImage' name='propertyImage' onchange='propertyImageChange(this)'>
    <a href='javascript:updatePropertyImage($id)' class='btn btn-primary'>Update Image</a>
</form>

Ajax 方法

function updatePropertyImage(id) 
{
    $.ajax({
        data:new FormData($("#edit-property-image")[0]),
        async:false,
        type:'post',
        processData: false,
        contentType: false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        method: 'POST', // Type of response and matches what we said in the route
        url: '/property/updateImage', // This is the url we gave in the route
        success: function(response){ // What to do if we succeed
            if(response.response == "success") {
                $('.modal').modal('hide');
                propertyDetails(id);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
            alert(errorThrown);
        }
    });
}

控制器 $id = $request->input('property');

    $this->validate($request,[
        'propertyImage' => 'image|max:1999|required'
    ]);

    $response = "failed";
    //Handle File Upload
    if($request->hasFile('propertyImage')){
        //Get Filename with extension
        $fileNameWithExt = $request->file('propertyImage')->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        //Get just extension
        $extension = $request->file('propertyImage')->getClientOriginalExtension();
        //Filename to store
        $fileNameToStore = $id.'_'.time().'.'.$extension;
        //Upload Image
        $path = $request->file('propertyImage')->storeAs('public/property_images', $fileNameToStore);

        $property = Property::find($id);
        $property->image_url = $fileNameToStore;
        $property->save();

        $response = "success";
    }

    return response()->json([
        'response' => $response,
    ]);  

【问题讨论】:

  • 我认为这是由于验证错误所以,请在浏览器中检查 laravel 验证。同时检查文件大小不大于 1999 根据 'propertyImage' => 'image|max:1999|required'

标签: php laravel iis-10


【解决方案1】:

我解决了这个问题。这实际上是因为我的 php.ini 文件没有指定默认的临时文件夹。一旦我添加了文件上传工作。

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2017-10-12
    • 1970-01-01
    • 2021-07-26
    • 2017-05-06
    • 2016-07-07
    • 2012-04-06
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多