【问题标题】:How do display the word document in asp.net page retrieved from SQL Server database如何在从 SQL Server 数据库检索的 asp.net 页面中显示 word 文档
【发布时间】:2014-01-25 06:01:35
【问题描述】:

如何在从 SQL Server 数据库表中检索的 asp.net 页面上显示 Word 文档?

我已经将word文档上传到数据库,请看代码,

现在我想在gridview中显示word文档详细信息,当我在gridview中单击word文档名称时,word文档应该显示在网页中。请帮助我如何做到这一点。

protected void btnUpload_Click(object sender, EventArgs e)
{
    string filePath = FileUpload1.PostedFile.FileName;
    string filename = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename);
    string contenttype = String.Empty;

    //Set the contenttype based on File Extension
    switch (ext)
    {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;

        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;

        case ".xls":
            contenttype = "application/vnd.ms-excel";
            break;

        case ".xlsx":
            contenttype = "application/vnd.ms-excel";
            break;

        case ".jpg":
            contenttype = "image/jpg";
            break;

        case ".png":
            contenttype = "image/png";
            break;

        case ".gif":
            contenttype = "image/gif";
            break;

        case ".pdf":
            contenttype = "application/pdf";
            break;
    }

    if (contenttype != String.Empty)
    {
        Stream fs = FileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        //insert the file into database
        string strQuery = "insert into tblFiles(Name, ContentType, Data)" +
                          " values (@Name, @ContentType, @Data)";

        SqlCommand cmd = new SqlCommand(strQuery);

        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;

        InsertUpdateData(cmd);

        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = "File Uploaded Successfully";
    }
    else
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;
        lblMessage.Text = "File format not recognised." +
                          " Upload Image/Word/PDF/Excel formats";
    }
}

private Boolean InsertUpdateData(SqlCommand cmd)
{
    SqlConnection con = new SqlConnection("user id=sa;password=123;database=Shashank;data source=Shashank");

    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;

    try
    {
        con.Open();

        cmd.ExecuteNonQuery();

        return true;
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        return false;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

【问题讨论】:

  • 它以字符串格式保存在数据库中?如果是这样,请将
    设置为 runat server 并将内容作为 . mydiv.innerhtml=内容;

标签: asp.net sql-server c#-4.0


【解决方案1】:

据我所知,您必须将 word 文档转换为 .html 才能将其显示在网页本身中。您可以查看这篇向您展示如何操作的文章。 http://www.c-sharpcorner.com/UploadFile/munnamax/WordToHtml03252007065157AM/WordToHtml.aspx

我想我会在上传时将 ms doc 文件转换为 HTML,然后将其与 .doc 或 .docx 一起存储。您将选择的 HTML 并将其显示在 iframe 中,如果用户想要下载它,您可以使用实际的 .doc 文件。

【讨论】:

    【解决方案2】:

    您可以查看 Martin Balliaw 的 this approach

    他使用 OpenXML 和 Linq 来查询 Word 文档的内容。你不会得到非常准确的布局,但这对你来说可能是一个好的开始。

    【讨论】:

      【解决方案3】:

      uploadUrl = "ftp://nicklibee-pc/proj1/proj1/UFiles/" 文件名 = 源文件 Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(uploadUrl), System.Net.FtpWebRequest) ''这里是我们必须上传的服务器路径。 ''clsRequest.Credentials = 新 System.Net.NetworkCredential("", "") ''用户名和密码 clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

                  Dim bFile As Byte() = System.IO.File.ReadAllBytes(sourcefile)
                 Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
      
                               clsStream.Write(bFile, 0, bFile.Length)
                  clsStream.Close()
                  clsStream.Dispose()
      

      【讨论】:

      • 请修正您的格式并为您的代码添加说明
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      相关资源
      最近更新 更多