如何在网格视图中管理文件上传控件如何在文件上传中获取文件路径 [英] how to manage file upload control inside grid view how to get the path of file in file upload

查看:73
本文介绍了如何在网格视图中管理文件上传控件如何在文件上传中获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好..
我在其中拥有gridview,我放置了文件上传控件和一个名为upload
的按钮 当我单击上载按钮时,我需要在上载控件中获取文件路径,将其保存在所需的位置
在这里我以这种方式尝试过

hi all..
i have gridview in this i placed file upload control and a button called upload
when i click upload button i need to get file path in upload control save it in desired location
here i tried in this way

<asp:GridView ID="GridView1" CellPadding="4" runat="server" OnRowCommand="GridView1_RowCommand"

                                                AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="5"

                                                Width="95%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting">
                                                <AlternatingRowStyle BackColor="aliceBlue" />
                                                <HeaderStyle HorizontalAlign="Left" />
                                                <Columns>
                                                    <asp:TemplateField Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label ID="eid" runat="server" Text='<%# Eval("uid") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:BoundField DataField="ccode" HeaderText="Company Code" />
                                                    <asp:BoundField DataField="cname" HeaderText="Company Name" />
                                                    <asp:TemplateField HeaderText="Company Logo">
                                                        <ItemTemplate>
                                                            <asp:Image ID="LogoImage" ImageUrl="~/images/logo-not-available.jpg" runat="server"

                                                                Height="80px" Width="150px" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField Visible="false">
                                                        <ItemTemplate>
                                                            <asp:Label ID="logopathlbl" runat="server" Text='<%# Eval("logopath") %>'></asp:Label>
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                    <asp:TemplateField HeaderText="Logo Upload">
                                                        <ItemTemplate>
                                                            <asp:FileUpload ID="FileUpload4" runat="server" />
                                                            <asp:Button ID="bt_upload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload" />
                                                        </ItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                            </asp:GridView>


当我从
之类的行命令后面的代码中调用时


when am calling from code behind in row command like

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button bts = e.CommandSource as Button;
        Response.Write(bts.Parent.Parent.GetType().ToString());
        if (e.CommandName.ToLower() == "upload")
        {
            FileUpload fu = bts.Parent.Parent.FindControl("FileUpload4") as FileUpload;//here it is detecting file upload4 

            if (fu.HasFile)//here it is showing no file in file upload control
            {
                Response.Write(fu.PostedFile.FileName);
            }
        }
    }


否则请告诉我其他方式.
这是我的bigggg问题,任何人都可以帮我吗
在此先感谢


or else show me any other ways..
this is my bigggg problem can any one help me please
thanks in advance

推荐答案

您好

应该可以了...
我尝试...

查看我编写并运行的完整代码:

在客户端代码中:
Hi

It should be working...
I try it...

See the entire code that I had made and run:

In Client code:
    <body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                    <asp:GridView ID="GridView1" CellPadding="4" runat="server" OnRowCommand="GridView1_RowCommand"
                        AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="5"
                        Width="95%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" EmptyDataText="No record in Company Table">
                        <AlternatingRowStyle BackColor="aliceBlue" />
                        <HeaderStyle HorizontalAlign="Left" />
                        <Columns>
                            <asp:TemplateField Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="eid" runat="server" Text=''<%# Eval("uid") %>''></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ccode" HeaderText="Company Code" />
                            <asp:BoundField DataField="cname" HeaderText="Company Name" />
                            <asp:TemplateField HeaderText="Company Logo">
                                <ItemTemplate>
                                    <asp:Image ID="LogoImage" ImageUrl="~/Images/mcts.JPG" runat="server" Height="80px"
                                        Width="150px" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="logopathlbl" runat="server" Text=''<%# Eval("logopath") %>''></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Logo Upload">
                                <ItemTemplate>
                                    <asp:FileUpload ID="FileUpload4" runat="server" />
                                    <asp:Button ID="bt_upload" runat="server" EnableViewState="False" Text="Upload" CommandName="Upload" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>




在我后面的代码中:




In my code behind:

   public class Company
{
    private string uid;
    private string ccode;
    private string cname;
    private string logopath;
    
    public string Uid { get { return uid; } set { uid = value; } }
    public string Ccode { get { return ccode; } set { ccode = value; } }
    public string Cname { get { return cname; } set { cname = value; } }
    public string Logopath{get { return logopath; }set { logopath = value; }}
    public Company()
    { }
}

public partial class Default2 : System.Web.UI.Page
{
    string myConn = ConfigurationManager.ConnectionStrings["TestConn"].ConnectionString;
    SqlConnection conn;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Company> coLst = new List<Company>();
            using (conn = new SqlConnection(myConn))
            {
                conn.Open();
                StringBuilder sbQry = new StringBuilder();
                sbQry.Append("SELECT * from Company ");
                using (cmd = new SqlCommand(sbQry.ToString(), conn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader odr = cmd.ExecuteReader())
                    {
                        while (odr.Read())
                        {
                            Company company = new Company();
                            {
                                company.Ccode = odr["uid"].ToString();
                                company.Ccode = odr["ccode"].ToString();
                                company.Cname = odr["cname"].ToString();
                                company.Logopath = odr["logopath"].ToString();
                            }
                            coLst.Add(company);
                        }
                    }
                }
            }
            GridView1.DataSource = coLst;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Button bts = e.CommandSource as Button;
        Response.Write(bts.Parent.Parent.GetType().ToString());
        if (e.CommandName.ToLower() != "upload")
        {
            return;
        }
        FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here 
        if (fu.HasFile)
        {
            bool upload = true;
            string fleUpload = Path.GetExtension(fu.FileName.ToString());
            if (fleUpload.Trim().ToLower() == ".xls" | fleUpload.Trim().ToLower() == ".xlsx")
            {
                fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
                string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
                //Someting to do?...
            }
            else
            {
                upload = false;
                // Something to do?...
            }
            if (upload)
            {
                // somthing to do?...
            }
        }
        else
        {
            //Something to do?...
        }
    }



刚刚修改了此版本以适合您的需求..

请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.

问候,

代数



Just revised this to fit in your needs..

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem




如果有帮助,请尝试此操作...
您的代码后面的示例:
Hi,

Try this if it could help ...
Example in your code behind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Button bts = e.CommandSource as Button;
    Response.Write(bts.Parent.Parent.GetType().ToString());
    if (e.CommandName.ToLower() != "upload")
    {
        return;
    }
    FileUpload fu = bts.FindControl("FileUpload4") as FileUpload;//here
    if (fu.HasFile)
    {
        bool upload = true;
        string fleUpload = Path.GetExtension(fu.FileName.ToString());
        if (fleUpload.Trim().ToLower() == ".xlsx")
        {
            fu.SaveAs(Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));
            string uploadedFile = (Server.MapPath("~/UpLoadPath/" + fu.FileName.ToString()));

            //Someting to do?...
        }
        else
        {
            upload = false;
            // Something to do?...
        }
        if (upload)
        {
              // somthing to do?...
        }
    }
    else
    {
        //Something to do?...
    }
}




您的应用程序域中必须有一个子目录 UpLoadPath ,而不是在您的目录中
复制此类文件的项目解决方案,以便在您的应用中显示.否则,这可能
在本地运行,但发布时可能会遇到找不到文件错误...

希望这可以帮助...

请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.

问候,

代数




You must have a Sub directory UpLoadPath in you apps domain rather in you
project solution to copy such file, to be visible in your apps. Otherwise this could
run in your local, but when it published you may encountered file not found error...

Hope this could help...

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem




选中此 [ [ ^ ]
Hi,

Check this[^]


and


This[^]


这篇关于如何在网格视图中管理文件上传控件如何在文件上传中获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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