Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - 提交大文件Laravel和Ajax(Submitting large files Laravel and Ajax)

I have a form where user submit and add product with multiple images to the database through Ajax request.

(我有一个表单,用户可以通过Ajax请求将具有多个图像的产品提交并添加到数据库中。)

The problem is when I choose a lower(12kb) image it save the product but if it is large file(1mb) or (400kb) it doesn't save the product.

(问题是,当我选择较低的(12kb)图像时,它将保存产品,但是如果文件较大(1mb)或(400kb),则不会保存产品。)

I have set the limit of upload to 10000mb, how can I fix this issue?

(我已将上传限制设置为10000mb,如何解决此问题?)

Controller

(控制者)

  public function store(Request $request)
{

   // ($request->all());
    $formInput=$request->except('filename');

    $product = product::create(array_merge($formInput, [
        'seller_id'=> Auth::user()->id,
        'user_id' => Auth::user()->id
    ]));
    foreach ($request->photos as $photo) {
       $filename = $photo->store('public/photos');
        ProductsPhoto::create([
            'product_id' => $product->id,
            'filename' => $filename
        ]);
    }

}

Ajax

(阿贾克斯)

 var token = $("#token").val();
 $(document).ready(function(){
 $("#btn").click(function(){
    var form = $('form')[0]; 
    var formData = new FormData(form);
    formData.append('_token', token); // adding token
    $.ajax({
        url: "<?php echo url('seller/product') ?>",
        data: formData, 
        type: 'POST',
        contentType: false, 
        processData: false,
        success:function(data){
         console.log(data);
        }
    });
  });
});
  ask by joh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use proper error handling and check for exceptions.

(使用正确的错误处理并检查异常。)

You should set both upload_max_filesize , post_max_size and memory_limit in your php.ini configuration.

(您应该在php.ini配置中同时设置upload_max_filesizepost_max_sizememory_limit 。)

Example

()

upload_max_filesize = 20M
post_max_size = 21M
memory_limit = 128M

NOTE : The post_max_size must be little larger than upload_max_filesize

(注意: post_max_size必须略大于upload_max_filesize)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...