简体   繁体   English

通过ajax上传图像时遇到问题

[英]Having issues uploading an image via ajax

Sorry if the title isn't descriptive enough! 抱歉,标题不够描述!

The idea is to have a div popup and allow users to type in their feedback and possibly submit a screenshot. 这个想法是要有一个div弹出框,并允许用户输入他们的反馈并可能提交屏幕截图。

This is the javascript inside the page (which is called via ajax into a div). 这是页面内的javascript(通过ajax调用到div中)。

Javascript Java脚本

$("#submitFeedback").click(function(){
  //Check if the form is valid
  if($("#form").valid()){
    $("#form").hide(); //Hide the form
    $("#feedbackReport").append("<h2 style='color:white'>Submitting!</h2><hr />" +
      "<div style='text-align: center'><img src='images/loader.gif' /></div>");//Display loading spinner
    $.ajax({  
      type: "POST",  
      url: "ajax/submit_feedback.php",
      cache: false,
      data: $("#form").serialize(),//serialize form for submission
      success: function(data) {
        $("#feedbackReport").empty();//Clear form after submission
        $("#feedbackReport").append("<h2>Thank you!</h2><span id='close'>&#10006;</span><hr />"
          +"Your feedback has been submitted successfully!"
          +"<br /><br /><span id='cancel'>Close</span>");
        $("#cancel,#close").click(function(){
          $("#feedbackReport").parent().hide();
        });
      }
    });
    return false;//Prevent page from changing
  }
});

Now, if point my browser to the actual ajax page (ajax/feedback.php) and submit an image, it works fine! 现在,如果将我的浏览器指向实际的ajax页面(ajax / feedback.php)并提交图像,则可以正常工作! When I run it from index.php and call the feedback window via ajax, it wont submit my picture, only the text. 当我从index.php运行它并通过ajax调用反馈窗口时,它不会提交我的图片,仅提交文本。

I know my submit_feedback.php page works fine because of this, I just have no idea why it won't post the image properly. 我知道我的Submit_feedback.php页面可以正常运行,因此,我根本不知道为什么它无法正确发布图像。

Any tips or help would be greatly appreciated! 任何提示或帮助将不胜感激!

Ajax can't do file uploads. Ajax无法上传文件。 You have to use something different such as https://github.com/valums/file-uploader 您必须使用其他内容,例如https://github.com/valums/file-uploader

Ajax does not allow file uploads however the neat trick is to use an iframe that holds the file upload form and auto submit it when a file is selected. Ajax不允许上传文件,但是一个巧妙的窍门是使用保存文件上传表单的iframe,并在选择文件后自动提交。

This way only the iframe is redirected and it gives it an ajax feel. 这样,只有iframe会被重定向,并给人以ajax的感觉。

Read more here 在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM