C# 保存远程文件到本地
///<summary>///保存远程文件到本地///</summary>///<paramname="url">远程文件URL</param>///<paramname="file">保存的本地路径</param>///<returns></returns>publicboolDownloadFile(stringurl,stringfile){try{(newSystem.Net.WebClient()).DownloadFile(url,file);returntrue;}catch{}returnfalse;}publicvoidDownload(stringstrURL,stringstrName){stringstrRootDir="D:\\DownLoadRecode";if(!Directory.Exists(strRootDir)){Directory.CreateDirectory(strRootDir);}WebClientclient=newWebClient();stringstrFileName=string.Empty;stringstrFileDir=string.Empty;stringstrSavePath=string.Empty;string[]arrName=strName.Split('/');if(arrName!=null&&arrName.Length>1){strFileDir=arrName[0];strFileName=arrName[1];strSavePath=strRootDir+"\\"+strFileDir;if(!Directory.Exists(strSavePath)){Directory.CreateDirectory(strSavePath);}}else{strFileName=strName;strSavePath=strRootDir+"\\Temp";if(!Directory.Exists(strSavePath)){Directory.CreateDirectory(strSavePath);}}strSavePath+="\\"+strFileName;if(!File.Exists(strSavePath)){client.DownloadFile(strURL,strSavePath);}FileInfoxFileInfo=newFileInfo(strSavePath);Response.Clear();//清除缓冲区流中的所有内容输出Response.ClearHeaders();//清除缓冲区中的所有头Response.Buffer=false;//设置缓冲输出为false//设置输出流的HTTPMIME类型为application/octet-streamResponse.ContentType="audio/x-wav";Response.Charset="GB2312";Response.ContentEncoding=Encoding.UTF8;//将HTTP头添加到输出流Response.AppendHeader("Content-Disposition","p_w_upload;filename="+HttpUtility.UrlEncode(strFileName));Response.AppendHeader("Content-Length",xFileInfo.Length.ToString());//将指定的字符直接写入HTTP内容输出流Response.WriteFile(strSavePath);Response.Flush();Response.End();}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。