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);

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集