dongtiao2105 2015-09-16 07:13
浏览 95

Laravel 5.1在包含base64编码图像的AngularJS帖子上返回错误301

Help, because I'm losing my mind.

I built an API using Laravel 5.1, and my app uses AngularJS (1.4.5) that communicates with the API. FabricJS plugin creates a Base64 encoded image from a canvas, and I want to send it using AJAX post method to a Laravel post route.

My post data in Javascript looks something like this:

var postData = {
    image: "data:image/png;base64,iVBORw0KGgoAAAA(...)",
    data: (some arrays and objects)

Now, when I post the data, it's about 2 MB with the image data. On post to Laravel route (post route, no trailing slashes), Laravel returns a 301 Moved Permanently (in Firefox 40.0.2), and in Google Chrome, Network tab displays 301 error followed by error 405 Method Not Allowed with an error in Laravel's RouteCollection.php on line 201.

Post JSON is valid (checked), and post works fine when I don't post the image data (parameter is set to image: null).

Server PHP version is 5.6.12.

Error seemingly gets output when I upload large JPEG files (like, compression 100 or 98). PHP settings says my upload limit is 20MB.

On the other hand, that same post works when the image is a JPEG, full HD, saved from Photoshop with Save for Web..., compression 80 with progressive option enabled.

WORKAROUND

Colleague and I tried this and it worked for us: for some reason, Laravel doesn't like full image data as a string, example:

data:image/png;base64,iVBORw0KGgoAAAA(...)

Solution is to break down that to actual base64 encoded string (in this case iVBORw0KGgoAAAA(...)) and from first part, and pass the file type as another parameter.

So the Javascript object (from top of post) would be something like this:

var postData = {
    image: "iVBORw0KGgoAAAA(...)",
    imageType: "png",
    data: (some arrays and objects)

Using PHP, we reassemble the entire image string (if "png" prepend "data:image/png;base64," to image) and (in our case) use Intervention Image plugin and save image to a directory on a server.

  • 写回答

1条回答 默认 最新

  • dpn68721 2015-09-30 22:39
    关注

    I had an issue passing sterilized XML.

    Try passing the string in a query string and then str_replace(' ', '+', $base64string);

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵