选择文件后YUI Uploader挂起

时间:2010-03-31 16:39:36

标签: yui yui-uploader

以下是来自包含YUI上传器的User控件的完整代码。有什么我想念的东西。现在,当我单步执行Firebug中的javascript代码时,它会挂起upload()函数的第一行。我在处理文件的ashx的第一行有一个断点,但它永远不会被调用。所以,它没有那么远。我想我只是错过了一些愚蠢的东西。我以前多次使用过这种控件而没有任何问题。我正在使用YUI下载中的samples文件夹提供的所有css文件和图形。

如果我没有遗漏任何东西,是否有更全面的方法来解决这个问题,然后通过使用FireBug逐步执行javascript。我已经尝试打开和关闭YUI的日志记录,并且永远不会在任何地方获取任何日志。我不知道现在该去哪里。

<style type="text/css">
   #divFile
   {
        background-color:White;
        border:2px inset Ivory;
        height:21px;
        margin-left:-2px;
        margin-right:9px;
        width:125px;
   }
</style>
<ajaxToolkit:RoundedCornersExtender runat="server" Corners="All" Radius="6" ID="rceContainer" TargetControlID="pnlMMAdmin" />
<asp:Panel ID="pnlMMAdmin" runat="server" 
 Width="100%" BackColor="Silver" ForeColor="#ffffff" Font-Bold="true" Font-Size="16px">
<div style="padding: 5px; text-align:center; width: 100%;">

<table style="width: 100% ; border: none; text-align: left;">
    <tr>
    <td style="width: 460px; vertical-align: top;">
        <!-- information panel -->
        <ajaxToolkit:RoundedCornersExtender runat="server" Corners="All" Radius="6" ID="RoundedCornersExtender1" TargetControlID="pnlInfo" />
        <asp:Panel ID="pnlInfo" runat="server" 
         Width="100%" BackColor="Silver" ForeColor="#ffffff" Font-Bold="true" Font-Size="16px">
        <div id="infoPanel" style="padding: 5px; text-align:left; width: 100%;">
        <table>
        <tr><td>Chart</td><td>
        <table><tr><td><div id="divFile" ></div></td><td><div id="uploaderContainer" style="width:60px; height:25px"></div></td></tr>
        <tr><td colspan="2"><div id="progressBar"></div></td></tr></table>
</td></tr>

    </table>

</div></asp:Panel>
<script type="text/javascript" language="javascript">
    WYSIWYG.attach('<%= txtComment.ClientID %>', full);
    var uploader = new YAHOO.widget.Uploader("uploaderContainer", "assets/buttonSkin.jpg");
    uploader.addListener('contentReady', handleContentReady);
    uploader.addListener('fileSelect', onFileSelect)
    uploader.addListener('uploadStart', onUploadStart);
    uploader.addListener('uploadProgress', onUploadProgress);
    uploader.addListener('uploadCancel', onUploadCancel);
    uploader.addListener('uploadComplete', onUploadComplete);
    uploader.addListener('uploadCompleteData', onUploadResponse);
    uploader.addListener('uploadError', onUploadError);
    function handleContentReady() {
        // Allows the uploader to send log messages to trace, as well as to YAHOO.log
        uploader.setAllowLogging(false);

        // Restrict selection to a single file (that's what it is by default,
        // just demonstrating how).
        uploader.setAllowMultipleFiles(false);

        // New set of file filters.
        var ff = new Array({ description: "Images", extensions: "*.jpg;*.png;*.gif" });

        // Apply new set of file filters to the uploader.
        uploader.setFileFilters(ff);
    }
    var fileID;
    function onFileSelect(event) {
        for (var item in event.fileList) {
            if (YAHOO.lang.hasOwnProperty(event.fileList, item)) {
                YAHOO.log(event.fileList[item].id);
                fileID = event.fileList[item].id;
            }
        }
        uploader.disable();

        var filename = document.getElementById("divFile");
        filename.innerHTML = event.fileList[fileID].name;

        var progressbar = document.getElementById("progressBar");
        progressbar.innerHTML = "Please wait... Starting upload.... ";
        upload(fileID);
    }
    function upload(idFile) {
        // file hangs right here. **************************
        progressBar.innerHTML = "Upload starting... ";
        if (idFile != null) {

            uploader.upload(idFile, "AdminFileUploader.ashx", "POST");
            fileID = null;
        }
    }
    function handleClearFiles() {
        uploader.clearFileList();
        uploader.enable();
        fileID = null;

        var filename = document.getElementById("divFile");
        filename.innerHTML = "";

        var progressbar = document.getElementById("progressBar");
        progressbar.innerHTML = "";
    }
    function onUploadProgress(event) {
        prog = Math.round(300 * (event["bytesLoaded"] / event["bytesTotal"]));
        progbar = "<div style=\"background-color: #f00; height: 5px; width: " + prog + "px\"/>";

        var progressbar = document.getElementById("progressBar");
        progressbar.innerHTML = progbar;
    }
    function onUploadComplete(event) {
        uploader.clearFileList();
        uploader.enable();

        progbar = "<div style=\"background-color: #f00; height: 5px; width: 300px\"/>";
        var progressbar = document.getElementById("progressBar");
        progressbar.innerHTML = progbar;
        alert('File Uploaded');
    }

    function onUploadStart(event) {
        alert('upload start');
    }

    function onUploadError(event) {
        alert('upload error');
    }

    function onUploadCancel(event) {
        alert('upload cancel');
    }

    function onUploadResponse(event) {
        alert('upload response');
    }

</script>

2 个答案:

答案 0 :(得分:1)

似乎进度条变量的名称不匹配:您在其他地方将其称为进度条,但在upload()函数中将其称为progressBar。

更大的问题是你在onFileSelect函数中定义了progressbar变量。因此,变量的范围有限,不应在其他任何地方访问。

查看是否将progressbar的定义移出那里(或者通过使用getElementById从DOM中新鲜地抓取它)并修复案例不匹配解决了你的问题。

答案 1 :(得分:1)

YUI 2.8在事件和上传器方面存在问题。除非您使用2.9事件和上传器,否则它将无法工作。我浪费的时间多于我想承认试图让2.8工作的时间。我希望这能节省一些时间。