0

我正在使用 HTML 标签制作文件上传器。我的表 Data 中有一个具有数据类型的列,varbinary(max)因此文件以二进制格式成功保存在数据库中。

我还在网格中显示文件列表,旁边有一个用于查看文件的图标。当我单击该图标时,它应该读取我的二进制格式数据,然后查看该文件。

我在代码后面上传的代码是:

    protected void Upload(object sender, EventArgs e)
    {           
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string contentType = FileUpload1.PostedFile.ContentType;

        using (Stream fs = FileUpload1.PostedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string query = "insert into FileUploader2 values (@Name, @ContentType, @Data)";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Name", filename);
                        cmd.Parameters.AddWithValue("@ContentType", contentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Context.Response.Write("Uploaded Successfully!");                           
                    }
                }
            }
        }

        Response.Redirect(Request.Url.AbsoluteUri);
    }

我也能够成功地读回我的数据。其代码如下:

    [System.Web.Services.WebMethod]
    public static void ShowDocument()
    {

        string filename = string.Empty;
        string contentType = string.Empty;
        byte[] bytes = null;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            con.Open();

            using (SqlCommand com = new SqlCommand("SELECT * FROM FileUploader2", con))
            {
                using (SqlDataReader reader = com.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        filename = (string)reader["Name"];
                        contentType = (string)reader["ContentType"];
                        bytes = (byte[])reader["Data"];
                    }
                }
            }
        }

        System.Web.HttpContext.Current.Response.ContentType = contentType;
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; 
        filename=" + filename);
        System.Web.HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
        System.Web.HttpContext.Current.Response.Flush();
}

现在阅读后,它还应该显示文件的内容或下载文件的链接,因为我正在使用附件。但它什么也没做。调试后它给了我正确的值。但为什么它不显示文件?我究竟做错了什么?据我所知,由于我使用的是内容处置,它应该自己显示。

4

1 回答 1

0

我认为你错过了这个部分......

    string strFileName = "FileName";//Here you have to set the File Name...

    if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "pdf")
    {
        Response.ContentType = "application/PDF";
    }
    else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "doc")
    {
        Response.ContentType = "application/msword";
    }
    else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "docx")
    {
        Response.ContentType = "application/msword";
    }
    else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xls")
    {
        Response.ContentType = "application/vnd.ms-excel";
    }
    else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "xlsx")
    {
        Response.ContentType = "application/vnd.ms-excel";
    }
    else if (strFileName.Substring(strFileName.LastIndexOf('.') + 1).ToLower() == "ppt")
    {
        Response.ContentType = "application/vnd.ms-powerpoint";
    }
    else if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "txt")
    {
        Response.ContentType = "text/plain";
    }
    else
    {
        Response.ContentType = "text/plain";
    }

    System.IO.FileInfo file = new System.IO.FileInfo(strFileName);

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());

    Response.WriteFile(file.FullName);
    Response.End();

编辑后,如果您的数据以字节为单位,请尝试上次:

public void byteArrayToImage(byte[] bytes)
    {
        if (byteArray != null)
        {
            MemoryStream ms = new MemoryStream(bytes);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms, false,  false);

            img.Save(Server.MapPath("Photo/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);//Set Your image Location...
            ms.Close();                
        }
    }
于 2020-02-17T11:42:05.077 回答