AJAX无法上传blob图像

时间:2016-12-30 15:58:48

标签: ajax

我在使用ajax上传blob时遇到问题。我尝试了很多选项而且没有用。

我必须说,在标准意义上使用php时,相同的变量(尽管名称不同)有效,但在使用ajax时却没有。

请帮忙

ajax代码是:

$(function() {

$("#upload").click(function() {
  // validate and process form here   

  var username = $("input#username").val();

    var title = $("input#title").val();

    var image = $("#image").get(0).files.item(0);

    var information = tinymce.get('blogcontent').getContent();

     var dt = new Date();

//   variable for blog date and time  
   var dateandtime = dt.toLocaleString();

var dataString = 'username=' + username + '&title=' + title + '&image=' + image + '&information=' + information + '&dateandtime=' + dateandtime;
$.ajax({
type: "POST",
url: "functions/insertblogpost.php",
data: dataString,
success: function() {

  $('#writeblog').html("<div id='message'></div>");
  $('#message').html("<h2>User account created!</h2>")
  .append("<p>Please go back to login.</p>")
  .hide()
  .fadeIn(1000, function() {
    $('#message').append("<a href='../../Mobileapptemplate.php'>Back</a>");
  });
}
});


return false;

  });
});

php脚本是:

$connection = mysqli_connect($dbserver, $dbusername, $dbpassword, $database);

        $username = $_POST[ 'username' ];

        $blogTitle = $_POST["title"];

        $blogContent = $_POST["information"];

        $blogpicturename = $_FILES["image"]["name"];

        $blogpicdata = mysqli_real_escape_string( $connection, file_get_contents($_FILES["image"]["tmp_name"]));

        $blogpictype = $_FILES['image']['type'];

        $dateAndTime = $_POST["dateandtime"];
        $result =  "INSERT INTO ct5006ho_users.$username ( postnumber, user, title, 
        picturename, picture, blogpost, dateandtime ) VALUES ( '', '$username', '$blogTitle','$blogpicturename', 
        '$blogpicdata','$blogContent', '$dateAndTime');";

//if (
mysqli_query($connection, $result);

建立连接IS并将所有其他数据上传到phpmyadmin创建的数据库。我已从代码中省略了这些细节。

1 个答案:

答案 0 :(得分:0)

以下是答案:

$(function(){

$("#upload").click(function() {
  // validate and process form here   
tinyMCE.triggerSave();
    var form = $('form')[0]; // 
    var formData = new FormData(form);


 $.ajax({
type: "POST",
url: "functions/insertblogpost.php",
 data: formData, // Data sent to server, a se
 contentType: false,       
  cache: false,             // To unable request pages to be cached
 processData:false,
success: function() { ....................

如果有人遇到类似问题,请使用此经验来帮助您。

注意 - tinyMCE.triggerSave(); - 发送tinyMCE数据。这是我在 - data:formData,

的原始问题

好吧,请你开口问我!