如何使用Ajax文件上传检查文件是否已经上传?
How to check the file is already uploaded or not by using Ajax File Upload?
我再次询问有关 Ajax 文件上传的问题.. T_T
我有一个使用 Ajax 文件上传的 ASP.NET 网络表单(使用 VB.NET)。每当我上传文件时,我都会更新我的数据库 table。当我拖入上传面板并单击上传按钮时,我正在检查文件是否已上传。
如果目标文件已经上传,我想显示我的错误标签,如 'the file is already uploaded' 。但是标签没有显示。我确实进行了调试以跟踪结果,并且该文件确实存在并且它通过了我的标签文本设置但没有显示在我的表单上。
我的代码的哪一部分是错误的?希望有人能指导我。
这是我的 asp 代码
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript">
//customize the drag panel
function AjaxFileUpload_change_text() {
Sys.Extended.UI.Resources.AjaxFileUpload_Upload = "Click Upload";
document.getElementsByClassName('ajax__fileupload_uploadbutton')[0].style.width = '100px';
}
</script>
<div style="width:40%;padding:25px;margin-left:200px">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnClientUploadCompleteAll="MyCompleteAll" ChunkSize="16384" AllowedFileTypes="pdf" MaximumNumberOfFiles="10" />
<asp:Button ID="cmdDone" runat="server" Text="Done" style="display:none" ClientIDMode="Static" />
<script>
function MyCompleteAll() {
$('cmdDone').click()
}
</script>
</div>
<asp:Label ID="lblmsg" runat="server" Text="" Width ="150px" style="color:red"></asp:Label><br />
</asp:Content>
.vb 代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "AjaxFileUpload_change_text();", True) //customize ajax panel
lblmsg.Text = "" //error display
End Sub
Protected Sub MyCompleteAll(sender As Object, e As AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
Dim filename As String = e.FileName.Split(".").First + "_" + fileupload + ".pdf"
Dim path As String = Server.MapPath("~/uploads/")
//to add a database table of files up-loaded.
Dim constr As String = CONN + g_schema
Using con As New MySqlConnection(constr)
con.Open()
'check file is already uploaded
Dim select_seq As String = "select fname from filetable where fname like '" +
e.FileName.Split(".").First + "%'"
Dim cmd As New MySqlCommand(select_seq, con)
Dim reader = cmd.ExecuteReader()
While reader.Read()
fname = reader(0).ToString
End While
con.Close()
//the file is already uploaded
If fname IsNot "" Then
lblmsg.Text = "Already uploaded. Please upload the other files."
Else
// Upload process code
End Sub
谢谢。
我建议您在文件上传完成事件中检查文件。
因此,我们然后可以建立一个存在的文件列表 - 您可以上传多个文件。
因此,我建议放入一个文本框来“保留”每个错误(重复)文件。
因此,让我们将重复文件持久化到 session() 中(我们不能在 3 个事件(开始、完成、全部完成)中使用页面上的控件)。
所以,我们有这个代码:
Dim DupList As New List(Of String)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("DupList") = DupList
txtDups.Visible = False
Else
DupList = Session("DupList")
End If
End Sub
然后说这个标记 - 文本框、网格视图,当然还有 FileUpload。
所以,说这个标记:
<div style="width:40%;padding:25px">
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnClientUploadCompleteAll="MyCompleteAll" ChunkSize="16384"
/>
<asp:Button ID="cmdDone" runat="server" Text="Done"
style="display:none" ClientIDMode="Static"/>
<asp:TextBox ID="txtDups" runat="server" TextMode="MultiLine" Width="523px"></asp:TextBox>
<script>
function MyCompleteAll() {
$('#cmdDone').click()
}
</script>
<asp:GridView ID="Gfiles" runat="server" CssClass="table" DataKeyNames="ID"></asp:GridView>
</div>
到目前为止 - 非常好,你有那个 JavaScript 按钮单击以在完成后返回我们非常重要和需要的最终 post。
因此,单个文件上传完成事件 - 如下所示:
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
' now code to add say to a database table of files up-loaded.
' but FIRST CHECK if the file already exists
Dim strSQL As String = "SELECT * from MyUpLoadFiles where FileName = @F"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@F", SqlDbType.NVarChar).Value = e.FileName
Dim rstFiles As DataTable = MyRstP(cmdSQL)
If rstFiles.Rows.Count > 0 Then
' the file exists - don't save, add to our already exist list
DupList.Add(e.FileName)
Session("DupList") = DupList
Else
' file is ok, new - save it
Dim strFileSave As String
strFileSave = Server.MapPath("~/Content/" & e.FileName)
AjaxFileUpload1.SaveAs(strFileSave)
' now add to database
Dim NewRow As DataRow = rstFiles.NewRow
NewRow("FileName") = e.FileName
NewRow("UpLoadTime") = Date.Now
NewRow("User_id") = 1
NewRow("Size") = e.FileSize
NewRow("SavePath") = Path.GetDirectoryName(strFileSave) ' get path only
rstFiles.Rows.Add(NewRow)
MyRstUpdate(rstFiles, "MyUpLoadFiles")
End If
End Sub
所以请注意上面我们如何跳过文件(如果存在)。当然,如果它确实存在,那么我们当然不会在 MyFilesUpLoad table.
中创建一个条目
好的,所以所有文件都上传了 - js 按钮点击触发,我们现在有了这个最终代码存根 运行:
Protected Sub cmdDone_Click(sender As Object, e As EventArgs) Handles cmdDone.Click
' this final code is triggered by the javascrpt "click" on
' all file uplaod done
' if there are some duplicates - dispay to user.
If DupList.Count > 0 Then
txtDups.Visible = True
For Each s As String In DupList
If txtDups.Text <> "" Then txtDups.Text &= vbCrLf
txtDups.Text &= s & " already exists - skipped and not uploaded"
Next
End If
' now addtonal code - maybe display gird of up-loaded files
Dim strSQL As String = "select * from MyUpLoadFiles where UpLoadTime >= @D"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@D", SqlDbType.DateTime).Value = Date.Today
Gfiles.DataSource = MyRstP(cmdSQL)
Gfiles.DataBind()
' hide up-loader
AjaxFileUpload1.Visible = False
End Sub
所以,它看起来像这样:
我们点击确定,现在我们have/see这个:
好的,现在让我们尝试重新上传两个存在的文件。
我们所以,我们会看到这个:
因此,我们保留“坏”列表,并且skip/don不在完成事件中保存文件。
这里是两个数据帮助例程 - 我认为不需要太多提示,因为必须一遍又一遍地键入一些简单的代码才能将数据输入 table - 所以我使用这两个帮助例程来拯救世界贫困和一些键盘
Public Function MyRstP(cmdSQL As SqlCommand) As DataTable
Dim rstData As New DataTable
Using cmdSQL
Using conn = New SqlConnection(My.Settings.TEST4)
cmdSQL.Connection = conn
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
Public Sub MyRstUpdate(rst As DataTable, strTable As String)
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand("SELECT * from " & strTable, conn)
Dim da As New SqlDataAdapter(cmdSQL)
Dim daU As New SqlCommandBuilder(da)
conn.Open()
da.Update(rst)
End Using
End Using
End Sub
编辑:检查“已确认”文件
跟进问题不仅是文件是否存在,而且在某些时候(或某种方式 - 尚未公开),用户可以选中复选框或更改上传文件的状态。如果文件还没有被确认,那么我们仍然允许上传。
所以,有问题的代码是这样的:
Dim strSQL As String = "SELECT * from MyUpLoadFiles where FileName = @F
AND Confirmed = 9"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@F", SqlDbType.NVarChar).Value = e.FileName
Dim rstFiles As DataTable = MyRstP(cmdSQL)
如果rstFiles.Rows.Count > 0 那么
' 文件存在。
' 文件已确认 - 不要保存,添加到我们已经存在的列表中
DupList.Add(e.FileName)
Session("DupList") = DupList
别的
' 文件没问题,新的 - 保存它 -(或未确认)
我再次询问有关 Ajax 文件上传的问题.. T_T
我有一个使用 Ajax 文件上传的 ASP.NET 网络表单(使用 VB.NET)。每当我上传文件时,我都会更新我的数据库 table。当我拖入上传面板并单击上传按钮时,我正在检查文件是否已上传。 如果目标文件已经上传,我想显示我的错误标签,如 'the file is already uploaded' 。但是标签没有显示。我确实进行了调试以跟踪结果,并且该文件确实存在并且它通过了我的标签文本设置但没有显示在我的表单上。 我的代码的哪一部分是错误的?希望有人能指导我。
这是我的 asp 代码
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript">
//customize the drag panel
function AjaxFileUpload_change_text() {
Sys.Extended.UI.Resources.AjaxFileUpload_Upload = "Click Upload";
document.getElementsByClassName('ajax__fileupload_uploadbutton')[0].style.width = '100px';
}
</script>
<div style="width:40%;padding:25px;margin-left:200px">
<asp:ScriptManager runat="server"></asp:ScriptManager>
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnClientUploadCompleteAll="MyCompleteAll" ChunkSize="16384" AllowedFileTypes="pdf" MaximumNumberOfFiles="10" />
<asp:Button ID="cmdDone" runat="server" Text="Done" style="display:none" ClientIDMode="Static" />
<script>
function MyCompleteAll() {
$('cmdDone').click()
}
</script>
</div>
<asp:Label ID="lblmsg" runat="server" Text="" Width ="150px" style="color:red"></asp:Label><br />
</asp:Content>
.vb 代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "AjaxFileUpload_change_text();", True) //customize ajax panel
lblmsg.Text = "" //error display
End Sub
Protected Sub MyCompleteAll(sender As Object, e As AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
Dim filename As String = e.FileName.Split(".").First + "_" + fileupload + ".pdf"
Dim path As String = Server.MapPath("~/uploads/")
//to add a database table of files up-loaded.
Dim constr As String = CONN + g_schema
Using con As New MySqlConnection(constr)
con.Open()
'check file is already uploaded
Dim select_seq As String = "select fname from filetable where fname like '" +
e.FileName.Split(".").First + "%'"
Dim cmd As New MySqlCommand(select_seq, con)
Dim reader = cmd.ExecuteReader()
While reader.Read()
fname = reader(0).ToString
End While
con.Close()
//the file is already uploaded
If fname IsNot "" Then
lblmsg.Text = "Already uploaded. Please upload the other files."
Else
// Upload process code
End Sub
谢谢。
我建议您在文件上传完成事件中检查文件。
因此,我们然后可以建立一个存在的文件列表 - 您可以上传多个文件。
因此,我建议放入一个文本框来“保留”每个错误(重复)文件。
因此,让我们将重复文件持久化到 session() 中(我们不能在 3 个事件(开始、完成、全部完成)中使用页面上的控件)。
所以,我们有这个代码:
Dim DupList As New List(Of String)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("DupList") = DupList
txtDups.Visible = False
Else
DupList = Session("DupList")
End If
End Sub
然后说这个标记 - 文本框、网格视图,当然还有 FileUpload。
所以,说这个标记:
<div style="width:40%;padding:25px">
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnClientUploadCompleteAll="MyCompleteAll" ChunkSize="16384"
/>
<asp:Button ID="cmdDone" runat="server" Text="Done"
style="display:none" ClientIDMode="Static"/>
<asp:TextBox ID="txtDups" runat="server" TextMode="MultiLine" Width="523px"></asp:TextBox>
<script>
function MyCompleteAll() {
$('#cmdDone').click()
}
</script>
<asp:GridView ID="Gfiles" runat="server" CssClass="table" DataKeyNames="ID"></asp:GridView>
</div>
到目前为止 - 非常好,你有那个 JavaScript 按钮单击以在完成后返回我们非常重要和需要的最终 post。
因此,单个文件上传完成事件 - 如下所示:
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs) Handles AjaxFileUpload1.UploadComplete
' now code to add say to a database table of files up-loaded.
' but FIRST CHECK if the file already exists
Dim strSQL As String = "SELECT * from MyUpLoadFiles where FileName = @F"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@F", SqlDbType.NVarChar).Value = e.FileName
Dim rstFiles As DataTable = MyRstP(cmdSQL)
If rstFiles.Rows.Count > 0 Then
' the file exists - don't save, add to our already exist list
DupList.Add(e.FileName)
Session("DupList") = DupList
Else
' file is ok, new - save it
Dim strFileSave As String
strFileSave = Server.MapPath("~/Content/" & e.FileName)
AjaxFileUpload1.SaveAs(strFileSave)
' now add to database
Dim NewRow As DataRow = rstFiles.NewRow
NewRow("FileName") = e.FileName
NewRow("UpLoadTime") = Date.Now
NewRow("User_id") = 1
NewRow("Size") = e.FileSize
NewRow("SavePath") = Path.GetDirectoryName(strFileSave) ' get path only
rstFiles.Rows.Add(NewRow)
MyRstUpdate(rstFiles, "MyUpLoadFiles")
End If
End Sub
所以请注意上面我们如何跳过文件(如果存在)。当然,如果它确实存在,那么我们当然不会在 MyFilesUpLoad table.
中创建一个条目好的,所以所有文件都上传了 - js 按钮点击触发,我们现在有了这个最终代码存根 运行:
Protected Sub cmdDone_Click(sender As Object, e As EventArgs) Handles cmdDone.Click
' this final code is triggered by the javascrpt "click" on
' all file uplaod done
' if there are some duplicates - dispay to user.
If DupList.Count > 0 Then
txtDups.Visible = True
For Each s As String In DupList
If txtDups.Text <> "" Then txtDups.Text &= vbCrLf
txtDups.Text &= s & " already exists - skipped and not uploaded"
Next
End If
' now addtonal code - maybe display gird of up-loaded files
Dim strSQL As String = "select * from MyUpLoadFiles where UpLoadTime >= @D"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@D", SqlDbType.DateTime).Value = Date.Today
Gfiles.DataSource = MyRstP(cmdSQL)
Gfiles.DataBind()
' hide up-loader
AjaxFileUpload1.Visible = False
End Sub
所以,它看起来像这样:
我们点击确定,现在我们have/see这个:
好的,现在让我们尝试重新上传两个存在的文件。
我们所以,我们会看到这个:
因此,我们保留“坏”列表,并且skip/don不在完成事件中保存文件。
这里是两个数据帮助例程 - 我认为不需要太多提示,因为必须一遍又一遍地键入一些简单的代码才能将数据输入 table - 所以我使用这两个帮助例程来拯救世界贫困和一些键盘
Public Function MyRstP(cmdSQL As SqlCommand) As DataTable
Dim rstData As New DataTable
Using cmdSQL
Using conn = New SqlConnection(My.Settings.TEST4)
cmdSQL.Connection = conn
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
Public Sub MyRstUpdate(rst As DataTable, strTable As String)
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand("SELECT * from " & strTable, conn)
Dim da As New SqlDataAdapter(cmdSQL)
Dim daU As New SqlCommandBuilder(da)
conn.Open()
da.Update(rst)
End Using
End Using
End Sub
编辑:检查“已确认”文件
跟进问题不仅是文件是否存在,而且在某些时候(或某种方式 - 尚未公开),用户可以选中复选框或更改上传文件的状态。如果文件还没有被确认,那么我们仍然允许上传。
所以,有问题的代码是这样的:
Dim strSQL As String = "SELECT * from MyUpLoadFiles where FileName = @F
AND Confirmed = 9"
Dim cmdSQL As New SqlCommand(strSQL)
cmdSQL.Parameters.Add("@F", SqlDbType.NVarChar).Value = e.FileName
Dim rstFiles As DataTable = MyRstP(cmdSQL)
如果rstFiles.Rows.Count > 0 那么 ' 文件存在。 ' 文件已确认 - 不要保存,添加到我们已经存在的列表中 DupList.Add(e.FileName) Session("DupList") = DupList 别的 ' 文件没问题,新的 - 保存它 -(或未确认)