1

Thank you for the consideration, I'm sorry for my non-informative question.

Actually I am using ajax and zend for uploading a file.

My ajax code looks like this:

$.ajax({
       type: "POST",
       url: "/business_general/imagesave",
       enctype: 'multipart/form-data',
       data: {'file': files.getAsBinary(), 'fname' : file.fileName},
       success: function(arrReturn){
          alert( "Data Uploaded: ");
       }
});

Here, I called a controller action(imagesave) to save my image in database

My Controller file action looks like this:

    $config = Zend_Registry::get('config');
    $vehiclelogo = $config->paths->vehiclelogo;
    $file = $objRequest->getParam('file');
    $ret = $objRequest->getParam('fname');
    $path_parts = pathinfo($ret);

    echo $path_parts['extension'], "\n";
    echo $path_parts['filename'], "\n";

    $targetPath = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));

    try {
        echo "POSTED FILE NAME"." ". $ret;
        echo "TYPE OF FILE UPLOADED"." "."-". gettype($ret);
        $strFilePath = $vehiclelogo.'/'.$targetPath.'.'.$path_parts['extension'];
        $OPfile = fopen($strFilePath,"w"); 
        fwrite($OPfile,$file);
        fclose($OPfile);
        echo "completed";
    }
    catch (Exception $e) {
        echo "error";
    }

Here, I am uploading the selected image into a folder.Acually, I am able to upload text files. But if I upload png/jpeg files,it gets uploaded into the folder, But the fact is that it could not be opened.

I should be able to able to upload every type of files.

How to execute this in zend-php and ajax?

1
  • Please improve the formatting of your code and take a look if you can't improve the wording of your question here or there. Commented Feb 28, 2013 at 13:57

1 Answer 1

2

Sorry .I think getAsBinary() doesn't support in modern browsers.you can use an invisible canvas for uploding files using ajax.

Example

 var canvas = document.getElementById("canvas")
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img,0,0);
      var strdata = canvas.toDataURL("image/png");
      document.getElementById("company_logo").src=strdata;
        $.ajax({
   type: "POST",                                                  
     url: "/business_vehicle/vehiclegeneralsave",                     
    data: "&data="+strdata,
    success: function(arrResult) {

//do something

    }
});

In controller side you can recive the image ` public function vehiclegeneralsaveAction() {

    $file    =  $arrVehicleDetails = trim($objRequest->getParam('data'));

      $strEncodedData = str_replace(' ', '+', $file);
    $strFilteredData = explode(',', $strEncodedData);
    $strUnencoded = base64_decode($strFilteredData[1]);

    file_put_contents('../public/image/image.png', $strUnencoded);
   }`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.