尝试使用AJAX的文件上传,但在损坏的PHP AJAX jQuery来上传文件但在、上传文件、文件上传、AJAX

2023-09-10 21:52:42 作者:極樂鬼

我试图使用PHP的ajax jQuery的文件上传,但在损坏的格式,我怎样才能让它正确的上传文件。

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

在我的控制,我使用

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

在AJAX文件

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照片查看器无法打开此图片。

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

请指导。

推荐答案

打开记事本等文本编辑器中的照片文件,你会发现,它可能包含p_image的字符串值。更具体地讲,你没有实际上传任何东西。我建议你​​使用像jQuery的当作ajaxForm上传与阿贾克斯。

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

教程:

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

文件:

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