带有下拉列表的文件上传控件 [英] fileupload control with dropdownlist

查看:82
本文介绍了带有下拉列表的文件上传控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向所有人致敬,
这是我在文件后面的代码:

Hiii to all,
This is my code behind file:

public partial class Fileupload : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd1; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Filldrop();
        }
    }
    private void Filldrop()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd=new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;       
        cmd.CommandText = "Select_File8";        
        cmd.Connection = conn;
        conn.Open();
        //cmd1.Parameters.Add("@FileName ", SqlDbType.VarChar).Value = FileUpload1.FileName; 
        // SqlDataAdapter da = new SqlDataAdapter(cmd);       
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        cmd.Parameters.Clear();   
        ad.SelectCommand = cmd;
        ad.SelectCommand.Parameters.Add("@FileName", SqlDbType.VarChar).Value = FileUpload1.FileName;    
        //cmd1.Parameters.Add("@FileID", SqlDbType.VarChar).Value = 1;
        DataSet ds = new DataSet();
        ad.Fill(ds,"Files");       
        //DropDownList1.DataTextField = "FileName ";
        //DropDownList1.DataValueField = "FileName ";
        DropDownList1.DataSource = ds;
        DropDownList1.DataBind();
        conn.Close();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.HasFile)
            {
                int filsize = FileUpload1.PostedFile.ContentLength;
                byte[] filebyt = new byte[filsize];
                FileUpload1.PostedFile.InputStream.Read(filebyt, 0, filsize);
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                cmd1 = new SqlCommand();
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.CommandText = "File_Upload";
                cmd1.Connection = conn;
                // SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();  
                cmd1.Parameters.AddWithValue("@FileName ", Path.GetFileName(FileUpload1.PostedFile.FileName));
                cmd1.Parameters.AddWithValue("@Files ", filebyt);               
                int result = cmd1.ExecuteNonQuery();
                conn.Close(); 
                cmd1.Dispose(); 
                conn.Dispose(); 
                filebyt = null;
                Response.Write("File uploaded");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string strname = string.Empty;
            string name = DropDownList1.SelectedItem.Value;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            cmd1 = new SqlCommand();
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.CommandText = "Select_File2";
            cmd1.Connection = conn;
            // SqlDataAdapter da = new SqlDataAdapter(cmd);
            conn.Open();   
            SqlDataReader dr = cmd1.ExecuteReader();
            while (dr.Read())
            {
                strname = dr["FileName "].ToString();
                Byte[] filbyt = (Byte[])dr["Files "];
                FileStream filstr = new FileStream(Server.MapPath("Documents")+ @"\" + strname, FileMode.OpenOrCreate);
                filstr.Write(filbyt, 0, filbyt.Length);
                filstr.Close(); 
                filstr.Dispose();
                filbyt = null;
            }
            conn.Close(); 
            cmd1.Dispose(); 
            conn.Dispose();
            if (strname != string.Empty)
            {
                Response.Redirect(@"Documents/" + strname);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    
}


这是我的存储过程:


This is my stored procedure:

ALTER PROCEDURE [dbo].[Select_File8]
AS
declare @FileName varchar(50)
BEGIN
    Select  FileName from Files
END


在这里,文件上传有效,但是我想通过存储过程在下拉列表中选择文件名来查看上传的文件,然后尝试打开所选的文档,该文件不起作用....


Here file upload works, but I want to view the uploaded file by selecting the filename in dropdownlist by means of stored procedure and try to open the selected document, its not working....

推荐答案

在DropDownList1的SelectedIndexChange事件上,编写以下代码

On the SelectedIndexChange event of the DropDownList1, write the following code

 bytDocTemp = (byte[])YOUROBJECT.DOCUMENTPROPERTY;
File_Name = YOUROBJECT.DOCUMENT_FileName;
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + File_Name + "\"");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytDocTemp);
Response.Flush();
Response.End();


也许是因为SP有问题,所以代码无法使用.
maybe youe code can not work because SP has problem.
ALTER PROCEDURE [dbo].[Select_File8]
AS
declare @FileName varchar(50)
BEGIN
    Select  FileName from Files
    Where @FileName=???
END



请再次检查.

太幸运了!



please check it again.

good lucky!


这篇关于带有下拉列表的文件上传控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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