0

我在下面有一个名为 cancelimage.php 的 php 脚本,它将在其中显示一条取消消息:

<?php

$image_file_name = $_FILES['fileImage']['name'] ;

    echo "$image_file_name Upload was Canceled";


?>

问题是当我单击“取消”按钮时,它不会显示此消息。相反,它显示消息“文件上传期间出错”。我相信这条消息出现的原因是因为我说的是“return stopImageUpload”。

所以我的问题是,我怎样才能像现在一样返回到 stopImageUpload,而是能够显示取消消息而不是现在显示的消息?

以下是我目前尝试解决此问题但失败的尝试。下面显示了取消按钮功能和 stopImageUpload 功能:

var cancelimagecounter = 0;
function startImageUpload(imageuploadform, imagefilename) {
    cancelimagecounter++;
    var _cancelimagecounter = cancelimagecounter;
    $('.imageCancel').on("click", function (event) {
        $('.upload_target').get(0).contentwindow
        $("iframe[name='upload_target']").attr("src", "javascript:'<html></html>'");
        jQuery.ajax("cancelimage.php?fileImage=" + image_file_name).done(function (data) {
            $(".imageemsg" + _cancelimagecounter).html(data);
        });
        return stopImageUpload();
    });
    return true;
}
var imagecounter = 0;
function stopImageUpload(success, imagefilename) {
    var result = '';
    if (success == 1) {
        result = '<span class="imagemsg' + imagecounter + '">The file was uploaded successfully!</span><br/><br/>';
        $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
    } else {
        result = '<span class="imageemsg">There was an error during file upload!</span><br/><br/>';
    }
    return true;
}
4

1 回答 1

0

前言:阿贾克斯不是我的强项.....

您正在依靠 php 来拉取文件名...但是 PHP 在启动上传之前无法获取文件名。如果要在不进行实际上传的情况下显示文件名,则需要从 DOM(输入按钮)中提取文件名。

$('input:file').change(function(){
    if ($(this).val()) {
    var filename = $(this).val().replace('C:\\fakepath\\', '');
    }
    $('.imageemsg').html('<p>this is the filename' + filename + '</p>');
});
于 2012-05-13T22:57:37.610 回答