3

嗨,在 AjaxFileUpload UploadComplete 事件之后无论如何都要更新(Gridview 或 Repeater 数据)。我想要做的是使用 AjaxFileUpload 上传多张图片,一旦文件上传,它应该将这些图片显示到 GridView 或 Repeater 控件中。

除非触发按钮单击事件,否则我无法执行此操作。

有任何想法吗???

4

4 回答 4

4

将隐藏按钮放在表单上并将此功能附加到OnClientUploadComplete扩展器的事件处理程序

<asp:Button runat="server" ID="HiddenButton" OnClick="RefreshGridView" style="display:none;" />

function uploadComplete(sender, args) {
    for (var index = 0; index < sender._filesInQueue.length; ++index) {
        if (!sender._filesInQueue[index]._isUploaded) {
            return;
        }
    }
    __doPostBack("<%= HiddenButton.UniqueID %>", "");
})

然后,单击此按钮刷新您的 GridView。

于 2013-02-12T17:00:48.750 回答
1

此代码检查上传的文件,创建包含文件信息的电子邮件,通过链接向文件的目标用户发送电子邮件。它还将所有信息存储到数据库中。上传页面上有一个网格视图,列出了所有已上传的文件。它在文件加载后更新。我认为你可以从中得到你需要的东西。

Partial Class upload_Default
Inherits System.Web.UI.Page

Protected Sub UploadButton2_Click(sender As Object, e As EventArgs)
    Dim fileGuid As String
    fileGuid = Guid.NewGuid.ToString


    If AsyncFileUpload1.HasFile Then
        If AsyncFileUpload1.FileContent.Length < 20971500 Then
            Try

                Dim fileSizeB As Integer = AsyncFileUpload1.PostedFile.ContentLength
                Dim fileSize = fileSizeB / 1024

                Dim filename As String = Path.GetFileName(AsyncFileUpload1.FileName)
                Dim fileNameTwo As String = Trim(fileGuid) + Trim(filename)

                Dim ExistPdfFilenamOPO As String
                ExistPdfFilenamOPO = Server.MapPath("~/uploads/files/") & filename

                If File.Exists(ExistPdfFilenamOPO) Then
                    Label2.Text = "File is already there"
                Else

                    Dim saveDir As String = "\Uploads\files\"
                    Dim appPath As String = Request.PhysicalApplicationPath
                    Dim savePath As String = appPath + saveDir + _
                    Server.HtmlEncode(AsyncFileUpload1.FileName)
                    AsyncFileUpload1.SaveAs(savePath)

                    UploadStatusLabel2.Text = "Upload status: File uploaded."
                    Label2.Text = ""

                    ' Email
                    Dim sr As New StreamReader(appPath & "EmailTemplates/FileUpload.htm")
                    Dim FName As String = TextBoxFName.Text
                    Dim LName As String = TextBoxLName.Text
                    Dim Email As String = TextBoxEmail.Text
                    Dim fullPath As String
                    fullPath = "https://website.com/uploads/default.aspx?fileGuid=" + fileGuid

                    Dim message As New MailMessage()
                    message.IsBodyHtml = True
                    message.From = New MailAddress("Your email")
                    message.[To].Add(New MailAddress(Email))

                    message.Subject = "The file you requested from SRTR"
                    message.Body = sr.ReadToEnd()
                    sr.Close()

                    message.Body = message.Body.Replace("<%FName%>", FName)
                    message.Body = message.Body.Replace("<%LName%>", LName)
                    message.Body = message.Body.Replace("<%Email%>", Email)
                    message.Body = message.Body.Replace("<%FileName%>", filename)
                    message.Body = message.Body.Replace("<%VerificationUrl%>", fullPath)

                    Dim client As New SmtpClient()
                    client.Send(message)

                    'Insert in to t_UploadFiles
                    Dim datenow As Date = System.DateTime.Now()
                    Dim ExDate As Date = datenow.AddDays(15)
                    Dim Downloaded As Boolean = False

                    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
                    Dim updateSql As String = "INSERT t_UploadFiles (FileGuid, FileName, FileSize, FName, LName, Email, UploadDate, ExDate, Downloaded) SELECT @FileGuid, @FileName, @FileSize, @FName, @LName, @Email, @UploadDate, @ExDate, @Downloaded"
                    Using myConnection As New SqlConnection(connectionString)
                        myConnection.Open()
                        Dim myCommand As New SqlCommand(updateSql, myConnection)
                        myCommand.Parameters.AddWithValue("@FileGuid", fileGuid.Trim())
                        myCommand.Parameters.AddWithValue("@FileName", filename.Trim())
                        myCommand.Parameters.AddWithValue("@FileSize", fileSize)
                        myCommand.Parameters.AddWithValue("@FName", FName.Trim())
                        myCommand.Parameters.AddWithValue("@LName", LName.Trim())
                        myCommand.Parameters.AddWithValue("@Email", Email)
                        myCommand.Parameters.AddWithValue("@UploadDate", datenow)
                        myCommand.Parameters.AddWithValue("@ExDate", ExDate)
                        myCommand.Parameters.AddWithValue("@Downloaded", Downloaded)

                        myCommand.ExecuteNonQuery()
                        myConnection.Close()
                    End Using
                    articleListXX.DataBind()

                End If

            Catch ex As Exception
                UploadStatusLabel2.Text = "Upload status: The file could not be uploaded.<br/>The following error occured: " + ex.Message
            End Try
        Else
            UploadStatusLabel2.Text = "File is too large."
        End If
    Else
        UploadStatusLabel2.Text = "You did not specify a file to upload."
    End If

End Sub

End Class
于 2013-02-12T19:01:41.560 回答
0

好的,谢谢你们的贡献,很抱歉让我的查询有点不清楚。我终于弄明白了,但这只是因为你的想法。这是我的代码。这同样适用于网格视图。主要目的是使用AjaxFileUpload 控件上传图片并在OnUploadComplete="AjaxFileUpload1_UploadComplete"事件中调用创建缩略图的方法。创建缩略图后,将populatePic()调用该方法在 javascript _doPostBack() 方法的帮助下将缩略图图像填充到转发器控件中,而无需用户触发按钮。

<script type="text/javascript">
        function showUploadedPic()
        {           
            __doPostBack('btnAdd', null);
        }
</script>

<cc1:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete" ThrobberID="myThrobber" MaximumNumberOfFiles="10" AllowedFileTypes="jpg,jpeg" OnClientUploadComplete="showUploadedPic" />


<asp:UpdatePanel ID="UpdatePanel1" runat="server">           
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl="<%# Container.DataItem %>" height="100"/>
                    </ItemTemplate> 
                </asp:Repeater>                
            </ContentTemplate>
            <Triggers>                
                <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>

代码背后

protected void btnAdd_Click(object sender, EventArgs e)
    {
        populatePic();         
    }



protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        string filePath = Server.MapPath("~/files/") + e.FileName;
        AjaxFileUpload1.SaveAs(filePath);
        createThumbnail();         
    }  
于 2013-02-12T19:40:08.070 回答
0

下面的代码经过测试并且可以工作。

<asp:Button runat="server" ID="HiddenButtonFileUpload" 

OnClick="RefreshGridView" style="display:none;" />

<ajax:AjaxFileUpload ID="AjaxFileUpload11" runat="server" 
MaximumNumberOfFiles="3" AllowedFileTypes="txt,xls,xlsx,doc,docx,pdf" 
Width="400px" 
                                    OnUploadComplete="OnUploadComplete" 
OnClientUploadStart="UploadStart" OnClientUploadCompleteAll="UploadComplete" 
ClearFileListAfterUpload="true" />

    function UploadComplete() {
        unblock();
        __doPostBack("<%= HiddenButtonFileUpload.UniqueID %>", "");
    }

 Code Behind : 

    protected void RefreshGridView(object sender, EventArgs e)
    {
        BindForm(); // refresh your gridview
    }
于 2018-02-14T10:29:05.730 回答