如何检查在ASP中重复的文件名:文件上传? [英] how to check the duplicate filename in asp:fileupload?

查看:91
本文介绍了如何检查在ASP中重复的文件名:文件上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UpdatePanel和它里面的ContentTemplate是一个asp:FileUpload..now时,我会尽量选择文件我想检查文件名中的database..i知道我的数据库的一部分,但我会怎么称呼在像ASP服务器功能:文本框,我可以使用OnTextChanged像that..but对ASP:文件上传有什么用,我可以检查文件名,不点击添加按钮?我的code

i have an updatepanel and inside of it the contenttemplate is an asp:FileUpload..now when i will try to choose the file i want to check the filename in the database..i know my database part but how will i call the function in the server like for asp:TextBox i can use OnTextChanged something like that..but for asp:FileUpload is there anything by which i can check the filename and without clicking the button add?? my code

<asp:UpdatePanel runat="server" ID="fileupdatepanel">
                <ContentTemplate>
            <asp:FileUpload ID="tutorialupload" runat="server" AutoPostBack="true" OnLoad="filename_Changed" />
                    <asp:Label runat="server" ID="f1"></asp:Label>
                    </ContentTemplate>
                </asp:UpdatePanel>
        <asp:Button ID="addbttu" runat="server" Text="Add" OnClick="addtutorial_Click" /> 

我aspx.cs code

my aspx.cs code

    protected void filename_Changed(object sender, EventArgs e)
{
string con = " ";
con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(con);
objsqlconn.Open();
string tuname = tutorialupload.PostedFile.FileName;
tuname = tuname.Substring(0, tuname.LastIndexOf("."));
SqlCommand objcmd = new SqlCommand("Select tutorialid from tutorialtable where tutorialname='" + tuname + "'", objsqlconn);
SqlDataReader grpIDreader = objcmd.ExecuteReader();
grpIDreader.Read();
if (grpIDreader.HasRows)
{
    f1.Text = "Duplicate filename.Sorry.";
}
else
{
    f1.Text = "";
}

objsqlconn.Close();
}

现在,当我将选择file..i要调用这个函数filename_Changed(),它会给我结果的文件名是present或not..so我想这样做没有点击我的添加按钮??

now when i will choose the file..i want to call this function filename_Changed() and it will give me the result whether the filename is present or not..so i want to do it without clicking my the add button??

推荐答案

下面是你如何检测使用JavaScript通过文件上传的onChange 事件选择的文件名更改。

Here is how you can detect the change in the selected file name using JavaScript through the FileUpload onChange event.

ASPX页面:确保设定的EnablePageMethods =真在的ScriptManager对象

ASPX Page: Make sure to set EnablePageMethods = true in the ScriptManager object.

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="fileupdatepanel">
    <ContentTemplate>
        <asp:FileUpload ID="tutorialupload" runat="server" OnChange="CheckFileName(this)" />
            <asp:Label runat="server" ID="f1"></asp:Label>
     </ContentTemplate>
 </asp:UpdatePanel>
 <asp:Button ID="addbttu" runat="server" Text="Add" OnClick="addtutorial_Click" /> 

JavaScript的:

function CheckFileName(oFile) 
{
    PageMethods.FileNameChecker(oFile.value, OnSucceeded); 
}

function OnSucceeded(result, userContext, methodName) 
{   
    lbl = document.getElementById('<%=f1.ClientID %>'); 

    if (methodName == "FileNameChecker") 
    { 
        if (result == true) 
        { 
            lbl.innerHTML = 'Duplicate filename.Sorry.'; 
            lbl.style.color = "red"; 
        } 
        else 
        { 
            lbl.innerHTML = ''; 
        } 
    } 
} 

C#code-背后::您可以拨打一个WebMethod,以检查是否在数据库中存在的新选择的文件名:

C# Code-Behind: You can call a WebMethod to check if the new selected filename exists in the DB:

您需要参考以下内容:

using System.Web.Services;

然后添加下面的方法,并确保你把 [的WebMethod] 方法声明:

Then add the following method and make sure you put [WebMethod] before method declaration:

[WebMethod]
public static bool FileNameChecker(string newFileName)
{
    string con = " ";
    con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

    SqlConnection objsqlconn = new SqlConnection(con);
    objsqlconn.Open();

    string x = newFileName.Substring(0,newFileName.LastIndexOf("\\"));
    string tuname = newFileName.Substring(newFileName.LastIndexOf("\\") + 1, newFileName.Length - x.Length - 1);
    tuname = tuname.Substring(0, tuname.LastIndexOf("."));

    SqlCommand objcmd = new SqlCommand("Select tutorialid from tutorialtable where tutorialname='" + tuname + "'", objsqlconn);
    SqlDataReader grpIDreader = objcmd.ExecuteReader();

    grpIDreader.Read();        

    bool found = false;

    if (grpIDreader.HasRows)
        found = true;

    objsqlconn.Close();
    return found;
} 

这篇关于如何检查在ASP中重复的文件名:文件上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆