在SQL Server'08数据库中上载和下载文件时出现的问题 [英] Problems in Uploading and Downloading files in SQL Server'08 database

查看:46
本文介绍了在SQL Server'08数据库中上载和下载文件时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在研究一个项目的消息模块,注册用户可以在这个模块中相互发送消息。在上传附件和发送消息的情况下,一切正常,但在INBOX模块中,下载附件时出现问题。以下是我用于上传和下载附件的代码。



上传和发送消息:

Hi Everyone,

I'm working on a messages module of a project in which registered users can send messages to each other. Everything works fine in case of uploading attachments and sending message but in INBOX module, there is a problem in downloading attachments. Below is the code which I'm using for uploading and downloading attachments.

Uploading and Sending Message:

string s = txtids.Text;
string[] eids = s.Split(',');

if (FileUpload1.HasFile)
            {
                //SqlDataSource1.Insert();
                string fileName = FileUpload1.PostedFile.FileName;
                string fileType = FileUpload1.PostedFile.ContentType;
                int contentLength = Convert.ToInt32(FileUpload1.PostedFile.InputStream.Length);
                byte[] bytContent = new byte[contentLength];
                int status = FileUpload1.PostedFile.InputStream.Read(bytContent, 0, contentLength);
                
                foreach (string eid in eids)
                {
                        cmd = new SqlCommand("insert into messages(sender_ID, receiver_ID, m_subject, m_content, file_name, file_extension, file_content) values ('" + Session["uid"] + "','" + eid.ToString().Trim() + "','" + txtsub.Text.Trim() + "','" + txtbody.Text.Trim() + "', @FileName, @Extension, @Content)", con);
                        SqlParameter prmFileName = new SqlParameter("@FileName", SqlDbType.NVarChar, 1000);
                        prmFileName.Value = fileName;
                        cmd.Parameters.Add(prmFileName);
                        SqlParameter prmContentType = new SqlParameter("@Extension", SqlDbType.NVarChar, 300);
                        prmContentType.Value = fileType;
                        cmd.Parameters.Add(prmContentType);
                        SqlParameter prmbytContent = new SqlParameter("@Content", SqlDbType.VarBinary);
                        prmbytContent.Value = bytContent;
                        cmd.Parameters.Add(prmbytContent);
                        cmd.ExecuteNonQuery();
                }
                Response.Redirect("compose_message.aspx");
            }
            else
            {
                String str = "N/A";
                byte[] arr = System.Text.Encoding.ASCII.GetBytes(str); 
                foreach (string eid in eids)
                {
                    cmd = new SqlCommand("insert into messages(sender_ID, receiver_ID, m_subject, m_content, file_name, file_extension, file_content) values ('" + Session["uid"] + "','" + eid.ToString().Trim() + "','" + txtsub.Text.Trim() + "','" + txtbody.Text.Trim() + "',@FileName, @Extension, @Content)", con);
                    SqlParameter prmFileName = new SqlParameter("@FileName", SqlDbType.NVarChar, 1000);
                    prmFileName.Value = str;
                    cmd.Parameters.Add(prmFileName);
                    SqlParameter prmContentType = new SqlParameter("@Extension", SqlDbType.NVarChar, 300);
                    prmContentType.Value = str;
                    cmd.Parameters.Add(prmContentType);
                    SqlParameter prmbytContent = new SqlParameter("@Content", SqlDbType.VarBinary);
                    prmbytContent.Value = arr;
                    cmd.Parameters.Add(prmbytContent);
                    cmd.ExecuteNonQuery();
                }
            }



下载文件的源代码:


Source Code for Downloading Files:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["E-PM"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Select file_extension, file_content from messages where m_ID = '" + Session["mid"] + "'", conn);
    conn.Open();

    SqlDataReader Reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    Reader.Read();
    Response.Clear();

    Response.ContentType = Reader["file_extension"].ToString();
    byte[] reportFile = (byte[])Reader["file_content"];

    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(reportFile);
    Response.Flush();
    Response.End();
    conn.Close();
}



我想要的是当用户点击链接按钮(下载)时,应打开文件保存对话框并询问用户保存文件的位置。



非常感谢任何形式的帮助。



谢谢


What I want is when user click on Link Button (Download), a file-save dialog box should open and ask user for saving location of file.

Any kind of help is very much appreciated.

Thank You

推荐答案

查看此博客

向/从SQL Server数据库上载和下载文件 [ ^ ]

--NDK
check this blog
Uploading and downloading files to/from a SQL Server database[^]
--NDK


我忘了添加一个属性:

I forgot to add one attribute:
Response.AddHeader("Content-Disposition", "attachment;filename=" + fname); 



所以,知道整个代码工作得非常好......:)


So, nnow the whole code works absolutely fine.. :)


这篇关于在SQL Server'08数据库中上载和下载文件时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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