最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pdf file upload ajax html - Stack Overflow

programmeradmin18浏览0评论
    var file = $('#image').prop('files')[0];
    var filename = $('#af_rpta_propertyland_filename').val();

    var form_data = new FormData();
    form_data.append('file', file);
    alert(form_data);


    $.ajax({
        type: 'POST',
        url: '../include/upload.php',
        //dataType: "json",
        data: {
            file: form_data,
            filename: filename
        },
        success: function(data) {
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                console.log("file " + i + ": " + data[i].file);
            }

        },
        error: function(data) {
            alert('No Record Found: ' + data);
        }


    });

<input id="image" name="image" type="file" />

This how i upload my pdf file using ajax in my php code i do it like this

$file = mysql_real_escape_string($_POST['file']);
$filename = mysql_real_escape_string($_POST['filename']);
    if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) { 
                $tmpName  = $_FILES['file']['tmp_name'];  
                $filetype = $_FILES['file']['type'];
                $fp = fopen($tmpName, 'rb'); // read binary
    $upload[] = array('filename' => $filename,'file' => $fp);

    }

echo json_encode($upload, JSON_UNESCAPED_UNICODE);

From my input(type file) how can i place the value(the pdf file) in to data(in ajax) and from data(ajax) how can i pass it to php file so that i can check if the $_files is not empty

    var file = $('#image').prop('files')[0];
    var filename = $('#af_rpta_propertyland_filename').val();

    var form_data = new FormData();
    form_data.append('file', file);
    alert(form_data);


    $.ajax({
        type: 'POST',
        url: '../include/upload.php',
        //dataType: "json",
        data: {
            file: form_data,
            filename: filename
        },
        success: function(data) {
            console.log(data);
            for (var i = 0; i < data.length; i++) {
                console.log("file " + i + ": " + data[i].file);
            }

        },
        error: function(data) {
            alert('No Record Found: ' + data);
        }


    });

<input id="image" name="image" type="file" />

This how i upload my pdf file using ajax in my php code i do it like this

$file = mysql_real_escape_string($_POST['file']);
$filename = mysql_real_escape_string($_POST['filename']);
    if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) { 
                $tmpName  = $_FILES['file']['tmp_name'];  
                $filetype = $_FILES['file']['type'];
                $fp = fopen($tmpName, 'rb'); // read binary
    $upload[] = array('filename' => $filename,'file' => $fp);

    }

echo json_encode($upload, JSON_UNESCAPED_UNICODE);

From my input(type file) how can i place the value(the pdf file) in to data(in ajax) and from data(ajax) how can i pass it to php file so that i can check if the $_files is not empty

Share Improve this question edited Jan 31, 2015 at 7:18 Brownman Revival asked Jan 29, 2015 at 4:39 Brownman RevivalBrownman Revival 3,8509 gold badges34 silver badges69 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 7 +50

Try creating a json object from files[0] properties , converting file to base64 string

js

$("#image").on("change", function(e) {
  var name = $("#af_rpta_propertyland_filename").val()
  , file = e.target.files[0]
  , filename = name.length > 1 ? name + ".pdf" : file.name
  , filetype = file.type
  , filesize = file.size
  , data = {
      "filename":filename,
      "filetype":filetype,
      "filesize":filesize
    }
  , reader = new FileReader();
    reader.onload = function(e) {
      data.file_base64 = e.target.result.split(/,/)[1];
        $.post("fileupload.php", {file:data}, "json")
        .then(function(data) {
          // parse `json` string `data`
          var filedata = JSON.parse(data)
          // do stuff with `data` (`file`) object
          , results = $("<a />", {
                "href": "data:" + filedata.filetype 
                        + ";base64," + filedata.file_base64,
                "download": filedata.filename,
                "target": "_blank",
                "text": filedata.filename
              });
          $("body").append("<br>download:", results[0]);
        }, function(jqxhr, textStatus, errorThrown) {
          console.log(textStatus, errorThrown)
        })
    };
    reader.readAsDataURL(file)
});

php

<?php
  if (isset($_POST["file"])) {
    // do php stuff
    // call `json_encode` on `file` object
    $file = json_encode($_POST["file"]);
    // return `file` as `json` string
    echo $file;
};

jsfiddle http://jsfiddle.net/guest271314/LL95z474/

Use jQuery version "jquery-1.10.2.min.js"

Use this AJAX

$.ajax({
        url: "YourPage.php", 
        type: "POST",             
        data: new FormData('YourFormId'), 
        contentType: false,                  
        processData:false,        
        success: function(data)   
        {
            // Do your Stuff
        }
    });

At PHP page just simply use this line

$name = $_FILES['file']['name'];

In this code i have used two new events

  1. contentType
  2. processData

This is necessary to use these to upload and access all data in AJAX.

Hope this will help you.

发布评论

评论列表(0)

  1. 暂无评论