如何在c#下载服务器之前预览图像 [英] How to preview an image before downloading from server in c#
本文介绍了如何在c#下载服务器之前预览图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我们在查询字符串中有文件ID时,如何在C#中从服务器下载之前预览图像。
我尝试过:
当我们在查询字符串中有文件id
解决方案时,如何在C#中从服务器下载之前预览图像
我试过相同的pdf
但不能在jpg中做同样的事情
string id = Request [id]。ToString(); //从网格点击查询字符串
string filename =;
string addr =;
string fpath =;
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings [OFcon]);
con.Open();
string query =select addr,filename from file =+ id +; < br $>
SqlCommand cmd = new SqlCommand(query,con);
SqlDataReader rd = cmd.ExecuteReader();
if(rd!= null)
{
if(rd.Read())
{
addr = rd.GetString(0);
filename = rd.GetString(1);
fpath = addr + filename;
}
}
con.Close();
字符串路径= Server.MapPath(。);
string path1 = path +\\+ fpath;
string viewpath = fpath;
string name = Path.GetFileName(fpath);
string ext = Path.GetExtension(fpath);
string FilePath = viewpath;
WebClient User = new WebClient();
Byte [] FileBuffer = User.DownloadData(FilePath);
if(FileBuffer!= null)
{
Response.ContentType =application / pdf;
Response.AddHeader( content-length,FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
How to preview an image before downloading from server in C# when we are having id of file in query string.
What I have tried:
how to preview an image before downloading from server in C# when we are having id of file in query string
解决方案
I have tried same for pdf
but not able to do same in jpg
string id = Request["id"].ToString(); // getting from grid click by query string
string filename = "";
string addr = "";
string fpath = "";
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["OFcon"]);
con.Open();
string query = "select addr,filename from files where id=" + id + "";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader rd = cmd.ExecuteReader();
if (rd != null)
{
if (rd.Read())
{
addr = rd.GetString(0);
filename = rd.GetString(1);
fpath = addr + filename;
}
}
con.Close();
string path = Server.MapPath(".");
string path1 = path + "\\" + fpath;
string viewpath = fpath;
string name = Path.GetFileName(fpath);
string ext = Path.GetExtension(fpath);
string FilePath = viewpath;
WebClient User = new WebClient();
Byte[] FileBuffer = User.DownloadData(FilePath);
if (FileBuffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", FileBuffer.Length.ToString());
Response.BinaryWrite(FileBuffer);
}
}
这篇关于如何在c#下载服务器之前预览图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文