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

时间:2014-01-25 06:01:35

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

如何在从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();
    }
}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

你可以看看Martin Balliaw的this approach

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

答案 2 :(得分:0)

uploadUrl =“ ftp:// nicklibee-pc / proj1 / proj1 / UFiles /” fileName =源文件 Dim clsRequest为System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(uploadUrl),System.Net.FtpWebRequest) ``这是我们必须上传的服务器路径。 ''clsRequest.Credentials = New 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()