文件名未定义

file name is undefined

本文关键字:未定义 文件名      更新时间:2023-09-26

对,这就是我想要的解决方案。当用户单击"取消"按钮时,它将尝试查找要取消的文件的文件名,然后删除包含该文件名的数据库行。

问题是它无法识别文件名,因为它指出imagecancelname=undefined .由于这是未定义的,因此它无法删除包含文件名的数据库行,因为它无法识别文件名。通过打印$imagecancelsql在我取消上传文件后,它会在下面显示以下内容:

DELETE FROM Image WHERE ImageFile = 'ImageFiles/undefined'

所以问题是,我怎样才能imagecancelname识别要取消的文件的名称,以便它可以用来删除包含文件名的数据库行?

以下是当前的表单代码:

var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' >" + 
  "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" + 
  "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +     
  "</p><p class='imagef1_cancel' align='center'></p>" +
  "<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");

下面是startImageUpload函数,它在文件上传时显示取消按钮,如果用户单击取消按钮,它包含取消按钮功能:

    var sourceImageForm; 
         function startImageUpload(imageuploadform, imagecancelname){
  $(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
  $(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
  $(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
  sourceImageForm = imageuploadform;
/*The above would show and hide elements within the form the file is being uploaded.
For example if I have three forms and the second form is uploading a file, then it
shows and hides the elements within the second form only */
        $(imageuploadform).find('.imagef1_cancel').html('<div><button type="button" class="imageCancel" id="image_cancel_' + imagecancelname + '">Cancel</button></div>').find(".imageCancel").on("click", function(event) {
        $.ajax("cancelimage.php?imagecancelname=" + $(this).attr('id').slice(13));
});       
      return true;
}

最后,下面是 cancelimage.php 脚本,当用户单击"取消"按钮时,它将导航到此脚本,这是脚本用于通过使用 $GET 方法查找文件名来删除数据库行的地方。

<?php
  // ... connected to DB
  $image_cancel_ = $_GET["imagecancelname"];
  $imagecancelsql = "
    DELETE FROM Image 
    WHERE ImageFile = 'ImageFiles/".mysql_real_escape_string($image_cancel_)."'
  ";
  print $imagecancelsql;
  mysql_close();
?>

以下是查询的打印:

注意:未定义的索引:imagecancelname in/web/stud/xxx/Mobile_app/cancelimage.php 第 19 行array(0( { } 从图像中删除,其中图像文件 = '图像文件/'

我认为在

完成上传之前,图像不应该存储在您的数据库中。如果图像被取消,那么它将不会在数据库中。

要回答未定义的确切问题,在jquery中,attr是获取标记属性。( ).

您应该在文件上传输入上放置一个 id:

<input id='fileImage' name='fileImage' type='file' class='fileImage' />

然后使用它来获取值

var image_cancel = $('#fileImage').val();

我的猜测是浏览器不喜欢您在按钮上定义的自定义属性来保存文件路径。但是,您可以轻松地将此信息存储在标准属性中 - id .试试这个 JS:

function startImageUpload(imagecancelname){
  // All this can be chained into one since .html() returns the correct jQuery object
  $('.imagef1_cancel').eq(window.lastUploadImageIndex).html('<div><button type="button" class="imageCancel" id="image_cancel_' + imagecancelname + '">Cancel</button></div>').find(".imageCancel").on("click", function(event) {
    $.ajax("cancelimage.php?imagecancelname=" + $(this).attr('id').slice(13));
  });     
  return true;
}