尝试通过Ajax上传文件时无法处理的实体

时间:2019-01-24 06:04:58

标签: php laravel iis-10

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

我已经尝试过赋予IUSER和IIS_IUSRS完全控制权限。我还尝试更新php.ini文件(file_uploads =启用,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,
    ]);  

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。实际上是因为我的php.ini文件未指定默认的临时文件夹。添加完文件后,就可以正常工作了。