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.


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


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 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?