尝试使用AJAX的文件上传,但在损坏的PHP AJAX jQuery来上传文件 [英] trying to upload a file using ajax file uploaded but in corrupted php ajax jquery

查看:213
本文介绍了尝试使用AJAX的文件上传,但在损坏的PHP AJAX jQuery来上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用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/

这篇关于尝试使用AJAX的文件上传,但在损坏的PHP AJAX jQuery来上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆