trying to upload a file using ajax file uploaded but in corrupted php ajax jquery

孤者浪人 提交于 2019-12-12 06:18:38

问题


I am trying to upload a file using PHP ajax jquery file is uploading but in corrupted format how can i make it correct.

in my controller i am using

$product_image = $request->getParam("product_image");

            defined('PUBLIC_PATH') || define('PUBLIC_PATH', realpath(dirname(dirname(dirname(dirname(dirname(__FILE__)))))));
            $filename = time() . rand(10000, 99999) . ".jpg";
            file_put_contents(PUBLIC_PATH . "/public_html/product_images/" . $filename, base64_decode($product_image));

$products->__set("product_image", $filename);

$data = array(
 "product_image" => $this->view->baseUrl() . "/product_images/" . $filename,
);

in ajax file

var p_image = $('#product_image').val();

$.ajax({


        type: "POST",
        url: '<?php echo $this->baseUrl(); ?>/api/products/add',
        data : {product_image:p_image},
                dataType: 'json',
        success: function(response){

            if (response.data.product_id == true) {
            alert("Success");

                        alert(response.data.product_image);

Getting the content of uploaded file is

windows photo viewer can't open this picture because the file appears to be damaged, corrupted, or is too large.

please guide.


回答1:


open the photo file in a text editor like Notepad and you'll find out that it likely contains the string value of p_image. More specifically, you're not actually uploading anything. I'd recommend using something like jQuery's ajaxForm to upload with ajax.

$('#myForm').ajaxForm(function() { 
         alert("Form is submitted"); 
});

Tutorial:

http://hayageek.com/ajax-file-upload-jquery/

Documentation:

http://malsup.com/jquery/form/



来源:https://stackoverflow.com/questions/32666132/trying-to-upload-a-file-using-ajax-file-uploaded-but-in-corrupted-php-ajax-jquer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!