简单的ASP.NET文件上载

时间:2013-02-18 17:23:05

标签: c# asp.net ajaxcontroltoolkit

我有一个非常简单的ASP.NET页面,可以上传Excel工作簿,然后对其进行处理。它使用ASP.NET上的AJAX工具包中的AJAXFILEUPLOAD ...这里是标记:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="ImportWorkbook.aspx.cs" Inherits="Timesheet.ImportWorkbook" %>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="HeaderContentPlaceHolder">
<h1 class="topContent">
    Upload CPAS Timesheet Workbooks
</h1>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
<br />
<br />
<asp:HiddenField ID="tbTSID" runat="server" />
<asp:HiddenField ID="tbWorkbookPath" runat="server" />
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" AllowedFileTypes="xls,xlsx,xlsm"
    CssClass="dropdown" MaximumNumberOfFiles="1" OnUploadComplete="AjaxFileUpload1_UploadComplete" />
<br />
<br />
<asp:Panel ID="ProcessChoices" runat="server" >
    <br />
    <br />
    <p>
        Select how you want this workbook processed:</p>
    <br />
    <asp:RadioButtonList ID="rbChoices" runat="server" BorderStyle="Groove" BorderWidth="2px"
        BorderColor="Black" BackColor="Teal" Font-Names="Tahoma" Font-Size="10pt" ForeColor="White"
        Width="40%">
        <asp:ListItem Value="True" Selected="True">&nbsp Replace ALL Items in the Timesheet</asp:ListItem>
        <asp:ListItem Value="False">&nbsp Add Items from this Workbook to the Existing Timesheet Items</asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:Button ID="btnValidate" runat="server" Text="Validate and Process" 
        BackColor="#B92217" ForeColor="White" BorderColor="#7C1810" 
        BorderStyle="Groove" Font-Names="Tahoma" onclick="btnValidate_Click" />
</asp:Panel>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="BottomSpanContentPlaceHolder" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</asp:Content>

母版页和CSS页面很简单,只有格式化。

这里是代码隐藏:

 using System;
using System.IO;
using TimesheetUtilites;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;

    namespace Timesheet
    {

    public partial class ImportWorkbook : System.Web.UI.Page
    {
        private const string HDriveLocation= "H:\\mtv\\secure\\Construction\\Access\\CPAS WorkArea\\TimesheetUploads\\";
        private string strWorkbookPath;    
        private int currTSID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    tbTSID.Value = Request.QueryString["ID"];            // Storing the Timesheet ID in a hidden Textbox                    
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(tbWorkbookPath.Value))
                {
                    ProcessChoices.Enabled = true;
                }
            }
            int.TryParse(tbTSID.Value, out currTSID);
            strWorkbookPath = tbWorkbookPath.Value;
        }
        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxFileUploadEventArgs e)
        {
            strWorkbookPath = HDriveLocation + Path.GetFileName(e.FileName);
            tbWorkbookPath.Value = strWorkbookPath;
            AjaxFileUpload1.SaveAs(strWorkbookPath);
            ProcessChoices.Enabled = true;
        }

        protected void btnValidate_Click(object sender, EventArgs e)
        {
            bool processOption;
            bool.TryParse(rbChoices.SelectedValue, out processOption);
            strWorkbookPath = tbWorkbookPath.Value;
            TimesheetUtilites.ImportTimesheet imp = new ImportTimesheet(currTSID, strWorkbookPath, processOption);       
        }
    }
}

我的问题很简单。虽然事件处理程序&#34; AjaxFileUpload1_UploadComplete&#34;工作正常,并在我触发&#34; btnValidate_Click&#34;时立即上传文件。事件,&#34; tbWorkbookPath.Value&#34;已成为一个空字符串,&#34; ProcessChoices.Enabled&#34;不会发生变化。毋庸置疑,&#34;上传完成&#34;事件处理程序是我必须捕获此文件路径的唯一机会,所以我不知道我做错了什么。

我在ASP.NET上发布并且没有答案。任何人都可以给我一个想法从哪里开始?

3 个答案:

答案 0 :(得分:0)

在页面上放置一个带有runat =“server”属性的隐藏字段,并使用以下脚本:

<script type="text/javascript">
    function uploadComplete(sender, args) {
            var filename = args.get_fileName();
            $("#hiddden_field_id").val(filename);
    }
</script>

现在您应该在活动中获取图像名称。

答案 1 :(得分:0)

我认为你应该尝试在会话中存储该值而不是隐藏字段,因为页面没有重新加载,而且它是一个ajax调用。因此,当单击按钮进行验证时,实际上是另一个请求,但此页面对象中隐藏字段的值和隐藏字段仍为空。在会话中为该值完成作业后,将其从那里删除或将其设置为某个不同的值。

答案 2 :(得分:0)

这是您应该在页面的ViewState中存储的信息,以便它在回发和页面初始化重置之间保持不变。将您的私有字符串成员更改为以下内容:

private string strWorkbookPath {
    get {
        return this.ViewState["strWorkbookPath"];
    }
    set {
        this.ViewState["strWorkbookPath"] = value;
    }
}

如果您需要有关ViewState的入门知识,请查看MSDN上的这篇文章:Saving Web Forms Page Values Using View State。它有点陈旧但仍传达了ViewState当前运作的基础知识。