如何在Framework7中使用Ajax调用上传文件?

时间:2018-12-06 10:14:59

标签: jquery html ajax html-framework-7

我正在使用Ajax调用在服务器上上传图像文件。但是,如果我使用了method =“ POST”形式,则工作正常,但是由于我想使用Ajax,那么该怎么做?

AJAX代码

function addcategory(){


      var cname = $("#categoryname").val();
      var desc =  $("#description").val();
      var img = $("#file1").val();
      var filename = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '')
      console.log(cname,desc);
    app.request({
        crossOrigin: true,
        type: "GET",
        url: "http://www.myreminde.com/addcategory.php",
        data:{category_name:cname,descri:desc},
        async:true,
        crossDomain:true,
        dataType: 'json',
        cache: true,     
        processData: false,
        success:function( response ) {
        if(response != 0){
                $("#img").attr("src",response); 
                $(".preview img").show(); // Display image element
            } else{
                alert('file not uploaded');
            }
        },


          }
      );

PHP代码

<?php header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Credentials: true');
  header('Access-Control-Allow-Methods: GET, POST, PUT');       
  $catname = $_GET['categoryname'];
  $description = $_GET['description'];
  $upload_image=$_FILES["myimage"]["name"];
  if (!empty($catname) || !empty($description)) {
$host = 'localhost';
$dbUsername = '';
$dbPassword = '';
$dbname = '';
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
 die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
 $dir="category/$catname";
mkdir($dir);
move_uploaded_file($_FILES["myimage"]["tmp_name"],"category/$catname/".$_FILES["myimage"]["name"]);
 $SELECT = "SELECT categoryName From category Where categoryName = ? Limit 1";
 $INSERT = "INSERT Into category (categoryName, categoryDescription, categoryImage) values('$catname','$description','$upload_image')";
 //Prepare statement
 $stmt = $conn->prepare($SELECT);
 $stmt->bind_param("s", $catname);
 $stmt->execute();
 $stmt->bind_result($categoryName);
 $stmt->store_result();
 $rnum = $stmt->num_rows;
 if ($rnum==0) {
  $stmt->close();
  $stmt = $conn->prepare($INSERT);
  $stmt->bind_param("sss", $catname, $description,$upload_image);
  $stmt->execute();

 } else {
  echo "Category already Present";
 }
 $stmt->close();
 $conn->close();
}}else{echo "All field are required";die();}?>

HTML代码 如果我使用表单方法post并将值发送到url,则可以正常使用,但会影响剩余的

`<form method="GET" action="http://www.myreminde.com/addcategory.php" 
 enctype="multipart/form-data" id="myform"><b>Category name:-</b>
<div class="item-input-wrap">
<input type="text" placeholder="category Name" name="categoryname" 
id="categoryname" required > </div> <b>Description:-</b>
<div class="item-input-wrap">
<input type="text" placeholder="description" name="description" 
id="description" required >
</div> <b>Category Image:-</b>
<input class="button button-small" type='file' id='file' name="myimage" 
accept="imags/*;capture=camera">
<button class="button" onclick="addcategory()">Create</button> 
</form>
</div>    
</div></div>`

这是我的html文件。

0 个答案:

没有答案