string filePath = dr["URL"].ToString();
string dir = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var fileType = dr["URL"].ToString();
fileType = Path.GetExtension(fileType);
//下载各种图片类型的
if (fileType == ".png" || fileType == ".gif" || fileType == ".jpeg" || fileType == ".bmp" || fileType == ".jpg" || fileType == ".tif" ||
fileType == ".PNG" || fileType == ".GIF" || fileType == ".JPEG" || fileType == ".BMP" || fileType == ".JPG" || fileType == ".TIF"
|| fileType == ".Jpeg")
{
WebClient mywebclient = new WebClient();
byte[] Bytes = mywebclient.DownloadData(dr["FULL_PATH"].ToString());
using (MemoryStream ms = new MemoryStream(Bytes))
{
Image outputImg = Image.FromStream(ms);
var img = outputImg;
img.Save(dr["url"].ToString());
}
}
//下载pdf类型的
else if (fileType == ".pdf" || fileType == ".PDF")
{
var f = dr["URL"].ToString();
Uri downUri = new Uri(dr["FULL_PATH"].ToString());
//建立一个web请求,返回HttpWebRequest对象
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
//流对象使用完后自动关闭
using (Stream stream = hwr.GetResponse().GetResponseStream())
{
//文件流,流信息读到文件流中,读完关闭
using (FileStream fs = File.Create(f))
{
//建立字节组,并设置它的大小是多少字节
byte[] bytes = new byte[102400];
int n = 1;
while (n > 0)
{
//一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
n = stream.Read(bytes, 0, 10240);
fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
}
}
}
}
else
{
MessageBox.Show("请检查该路径扩展名" + dr["FULL_PATH"].ToString());
}
08-20
2460
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)