回发后保留FileUpload控件的路径

时间:2010-09-30 09:55:23

标签: asp.net file-upload

我在UpdatePanel中有一个FileUpload控件和一个DropDownlist控件,当用户为FileUpload控件选择一个文件(还没有上传)时,同时用户从DropDownList控件中选择一个选项,这将导致回发!一旦页面回发,FileUpload控件中选择的路径将消失。如何保留FileUpload控件中的路径?文件上传功能正常。我希望在回发期间可以保留FileUpload控件中的路径。

我已尝试过以下解决方案,但“FileUpload1.HasFile”会向我返回false。

            If Session("FileUpload1") Is Nothing AndAlso Upload.HasFile Then
                Session("FileUpload1") = Upload
                lblPhotoUploadErr.Text = Upload.FileName
            ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not Upload.HasFile) Then
                Upload = DirectCast(Session("FileUpload1"), FileUpload)
                lblPhotoUploadErr.Text = Upload.FileName
            ElseIf Upload.HasFile Then
                Session("FileUpload1") = Upload
                lblPhotoUploadErr.Text = Upload.FileName
            End If

但下面的上传功能中的“Upload.HasFile”在执行时会为真。

Public Sub uploadPhoto()
    Dim FileOK As Boolean = False
    Dim FileSaved As Boolean = False
    Dim CandidateCode As String = Nothing
    Dim newFileName As String = Nothing

    Dim extension As String = Nothing
    Dim fileNameWithoutExt As String = Nothing

    If txtCandidateCode.Text.Trim <> "" Then
        CandidateCode = txtCandidateCode.Text.Trim
    End If

    If Upload.HasFile Then
        Dim FileExtension As String = Path.GetExtension(Upload.FileName).ToLower
        Dim allowedExtensions() As String = {".png", ".jpeg", ".jpg", ".gif"}

        Dim i As Integer = 0
        Do While (i < allowedExtensions.Length)
            If (FileExtension = allowedExtensions(i)) Then
                FileOK = True
            End If
            i = (i + 1)
        Loop
    End If

    If FileOK Then
        Try
            fileNameWithoutExt = Path.GetFileNameWithoutExtension(Upload.FileName)
            extension = Path.GetExtension(Upload.FileName)
            newFileName = fileNameWithoutExt + "_" + CandidateCode + extension

            Upload.PostedFile.SaveAs((path1 + newFileName))
            FileSaved = True
        Catch ex As Exception
            lblPhotoUploadErr.Text = ("File could not be uploaded." + ex.Message.ToString)
            FileSaved = False
        End Try
    Else
        lblPhotoUploadErr.Text = "Cannot accept files of this type."
    End If

    If FileSaved Then
        pnlUpload.Visible = False
        imgPhoto.ImageUrl = ("~/images/" + newFileName)
        hfPhotoUploadPath.Value = ("~/images/" + newFileName)

        hfFileExtension.Value = extension
        hfPhotoUploadFileName.Value = fileNameWithoutExt
    End If
End Sub

6 个答案:

答案 0 :(得分:10)

如果你从UpdatePanel中取出它,FileUpload只会保留它的值。这样你仍然可以使用DropDownList及其AutoPostBack完成所有操作,但是ajax-postback不会刷新FileUpload,导致它变空。这样你就不再需要postbacktriggers了。

仅将UpdatePanel放在DropDownList左右,并且回发必须更改的任何控件。如果这些控件不是彼此相邻,则可以使用多个UpdatePanel,AutoPostBack将刷新所有这些(默认行为,您甚至可以更改它)。

答案 1 :(得分:3)

dropDown是否需要回发?我认为出于安全原因,fileupload在updatePanels中不起作用。见这里:

http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx

答案 2 :(得分:2)

我想我找到了一个解决方案:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["FileUpload1"] = null;
    }
    else
    {
        if (FileUpload1.HasFile)
        {
            Session["FileUpload1"] = FileUpload1;
            TextBox1.Text = FileUpload1.FileName;
        }
        else if (Session["FileUpload1"] != null)
        {
            FileUpload1 = (FileUpload)Session["FileUpload1"];
            TextBox1.Text = FileUpload1.FileName;
        }
    }
}

<span class="spanFu">
    <asp:TextBox ID="TextBox1" Text="Select a file..." runat="server" CssClass="txt" ReadOnly="true" />
    <asp:FileUpload ID="FileUpload1" runat="server" onchange="File_OnChange(this)" CssClass="fu" />
</span>

<script>
function File_OnChange(sender) {
    val = sender.value.split('\\');
    document.getElementById('<%= TextBox1.ClientID %>').value = val[val.length - 1];
}
</script>

<style>
.spanFu .txt { width: 200px; height: 20px; }
.spanFu { position: relative; overflow: hidden; vertical-align: top; }
.fu { z-index: 1; width: 200px; height: 24px; position: absolute; top: 0px;
    left: 0px; filter: alpha(opacity=0); opacity: .0; }
</style>

答案 3 :(得分:2)

将您的fileupload控件放在Updatepanel之外,以便下拉列表引起的异步回发不会影响FileUpload控件。示例(简化):

<asp:FileUpload runat="server" />

<asp:UpdatePanel runat="server">
    <asp:dropdownlist runat="server" autopostback="true" />
</asp:UpdatePanel>

<asp:button runat="server" text="Submit" />

附加说明:您似乎在会话中存储FileUpload控件。这不是一个好主意,可能会给你带来一些问题,例如:当您的用户使用多个浏览器选项卡/窗口打开同一页面时。而且,我认为您可能想要做的是保存fileupload控件的文件名/ filebyte /其他属性,而不是整个控件存储到会话中更多的服务器资源。

答案 4 :(得分:1)

将UpdatePanel模式设置为条件,并将另一个UpdatePanel包装在Dropdown中。这样下拉列表就不会发布文件。

如果你想进行异步文件上传,你将无法上传,但你可以伪造它。

查看this project,它会更改form的目标,因此您网页上的任何内容都不会更改,它会发布到iframe。

答案 5 :(得分:-1)

为什么不在用户提交表单时禁用下拉列表。

  • 像...一样的东西。

    OnClientClick =“$('dropdown')。attr('disabled',true); return true;”

按钮上的