显示来自SQL/C#&;的文件;ASP.net
嗨,我不是在要求一个完全疯狂的ecode,更多的是一种方法,一个想法,一个“如何做”: 我的SQL server中有一些文件,pdf,txt。。。我想显示它们: 以下是从SQL server获取文件的代码:显示来自SQL/C#&;的文件;ASP.net,c#,asp.net,sql-server,C#,Asp.net,Sql Server,嗨,我不是在要求一个完全疯狂的ecode,更多的是一种方法,一个想法,一个“如何做”: 我的SQL server中有一些文件,pdf,txt。。。我想显示它们: 以下是从SQL server获取文件的代码: public Byte[] readFichier(string idFichier) { connexionString = getConnexionString(); // la commande à executer sur SQL
public Byte[] readFichier(string idFichier)
{
connexionString = getConnexionString();
// la commande à executer sur SQL
// on recupere le nom de la table dans web.config
string table = WebConfigurationManager.AppSettings["tableFichiers"];
string requestString = "SELECT * from " + table + " where FichierID=@FichierID";
// ouverture de la connection
SqlConnection connection = new SqlConnection(connexionString);
SqlCommand command = new SqlCommand(requestString, connection);
// on ajoute la valeur au parametre
command.Parameters.AddWithValue("@FichierID", idFichier);
DataTable tableDeDonnee = new DataTable();
SqlDataAdapter donneesSQL = new SqlDataAdapter();
donneesSQL.SelectCommand = command;
donneesSQL.Fill(tableDeDonnee);
// recuperation du fichier
Byte[] bytes = (Byte[])tableDeDonnee.Rows[0]["Fichier"];
return bytes;
}
然后我将其转换为html标签
[WebMethod]
public static string lireFichierStatique(string id)
{
var page = (WebForm1)HttpContext.Current.CurrentHandler;
Byte[] fichier=page.readFichier(id);
string stringFichier = Convert.ToBase64String(fichier, 0, fichier.Length);
System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
label.Text = stringFichier;
// transform the string into html code
var stringWriter = new StringWriter();
using (var htmlWriter = new HtmlTextWriter(stringWriter))
{
label.RenderControl(htmlWriter);
}
string labelString = stringWriter.ToString();
// return on line for more libibility
for (int i = 40; i < labelString.Length; i+=30 )
{
labelString=labelString.Insert(i, "<br>");
}
return labelString;
}
新编辑:
对于.txt很好,我会查看其他文件(我想我还需要另一个东西,antother lib,任何建议都很好,但核心问题已经得到了回答)
上次编辑:
public string pdfByteToString(Byte[] fichier)
{
string fichierString = "";
PdfReader reader = new PdfReader(fichier);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
fichierString+=iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
}
return fichierString;
}
公共字符串pdfByteToString(字节[]fichier)
{
字符串fichierString=“”;
PdfReader读取器=新PdfReader(菲希尔);
对于(int i=1;i如果您不想为所有这些类型编写内置浏览器,您可以建议用户下载文件。我已经成功地让用户下载或上传文件,但这里的想法是,在下载之前只显示文件的摘要。如果文件包含文本内容,请使用文件编码的任何编码(如果您不确定,UTF8是一个很好的起点)从字节中获取字符串。Encoding.UTF8.GetString(byte[])
。如果它是PDF,您需要使用其他内容。谢谢!它似乎适用于.txt,带有一些� 但它是可读的:我会为其他文件查看如果你不想为所有这些类型编写浏览器浏览器,你可以建议用户下载文件。我已经成功地让用户下载文件,或上传文件,但我的想法是,在下载之前只显示文件的摘要。如果文件包含文本内容,请使用任何enc编辑文件的编码(如果您不确定,UTF8是一个很好的起点)以从字节中获取字符串。Encoding.UTF8.GetString(byte[])
。如果它是PDF,您需要使用其他内容。谢谢!它似乎适用于.txt,带有一些� 但它是可读的:我将查看其他文件
public string pdfByteToString(Byte[] fichier)
{
string fichierString = "";
PdfReader reader = new PdfReader(fichier);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
fichierString+=iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i);
}
return fichierString;
}