谁有,c#写的 上传 下载文件 的原代码,急用,谢谢!在线等待 谁有,c#写的 上传 下载文件 的原代码,急用,谢谢!在线等待 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //下载文件方法 private void filedownload(string fullfilename) { FileInfo downloadfile = new FileInfo(fullfilename); Response.Clear(); Response.ClearHeaders(); Response.Buffer=false; Response.ContentType="application/octe-stream"; Response.AppendHeader("呵呵","文件名="+HttpUtility.UrlDecode(downloadfile.FullName,System.Text.Encoding.ASCII)); Response.AppendHeader("文件长度",downloadfile.Length.ToString()); Response.WriteFile(downloadfile.FullName); Response.Flush(); Response.End(); }filedownload(listfile.SelectedItem.Text); //下载文件 上传文件private void Button3_Click(object sender, System.EventArgs e) { if(textfileup.PostedFile.FileName=="") { lbinfo.Text="请选择你要上传的文件!!"; return; } try { char[] spliter={'\\'}; string [] filename = textfileup.PostedFile.FileName.Split(spliter,10); string fullpath = currentpath+@"\"+filename[filename.Length-1];//生成完整的文件名 textfileup.PostedFile.SaveAs(fullpath); //保存文件 loaddir(currentpath); //重新载入当前目录 } catch { lbinfo.Text="对不起,上传文件失败!"; } public static bool UploadFile(HtmlInputFile file,string fileName) { string filePath = System.Configuration.ConfigurationSettings.AppSettinsUploadAddres"]; if(!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } filePath += "\\"+fileName; try { file.PostedFile.SaveAs(filePath); return true; } catch { return false; } } public static void DownloadFile(string FileID) { string FileName = DocManageBll.GetDeptFileName(FileID); string physicalFilePath = System.Configuration.ConfigurationSettings.AppSettings["UploadAddres"]+"\\"+FileName; if(FileName=="" || GetDeptFileShowName=="") return ; try { stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); int bufSize = (int)stream.Length; byte[] buf = new byte[bufSize]; int bytesRead = stream.Read(buf, 0, bufSize); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(GetDeptFileShowName,System.Text.Encoding.UTF8) + "\""); HttpContext.Current.Response.End(); stream.Close(); } finally { } } http://blog.csdn.net/CQP/archive/2006/08/07/1034079.aspx 大哥们,我要得不是asp中的上传和下载,我是在.net下做的c/s结构,文挡管理中的把电子文档放到数据库中,请各位帮帮忙 private static void SaveBinaryFile(WebResponse response) { byte[] buffer=new byte[1024]; string filename=@"c:\temp.exe"; Stream oustr=File.Create(filename); Stream instr=response.GetResponseStream(); int i; do { i=instr.Read(buffer,0,buffer.Length); if(i>0) oustr.Write(buffer,0,i); } while(i>0); oustr.Close(); instr.Close(); System.Diagnostics.Process.Start(filename); } private static bool GetBinaryFile() { HttpWebRequest request=null; HttpWebResponse response=null; Stream stream=null; StreamReader reader=null; string URL=@"http://126.com"; try { Uri uri=new Uri(URL); request=(HttpWebRequest)WebRequest.Create(uri); response=(HttpWebResponse)request.GetResponse(); if(!request.HaveResponse) { return false; } if(response.StatusCode!=HttpStatusCode.OK) { return false; } SaveBinaryFile(response); return true; } catch(Exception err) { return false; } finally { if(reader!=null) reader.Close(); if(stream!=null) stream.Close(); if(response!=null) response.Close(); } } 有关属性访问器的问题 进来看看吧 WPF程序的相关问题 C#如何读取远程unix服务器文件夹下所有文件信息。谢谢 问个配置文件输出目录的问题。 请教各位C#要学到什么程度?哪个方面重要?在学的和学完的进来讨论一下 webBrowser1.DocumentText替换后,编码、样式发生变化 正则表达式遇到问题,高手进 救命啊 !!!!!!!!!!!!!! [求助]请问控件有没有(或类似)form的OnLoad()事件的?应如何实现? socket如何处理客户端传来的消息 有没有关于农药方面网站的源代码? 关于数据库连接问题-在线等
private void filedownload(string fullfilename)
{
FileInfo downloadfile = new FileInfo(fullfilename);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octe-stream";
Response.AppendHeader("呵呵","文件名="+HttpUtility.UrlDecode(downloadfile.FullName,System.Text.Encoding.ASCII));
Response.AppendHeader("文件长度",downloadfile.Length.ToString());
Response.WriteFile(downloadfile.FullName);
Response.Flush();
Response.End();
}
filedownload(listfile.SelectedItem.Text); //下载文件
{
if(textfileup.PostedFile.FileName=="")
{
lbinfo.Text="请选择你要上传的文件!!";
return;
}
try
{
char[] spliter={'\\'};
string [] filename = textfileup.PostedFile.FileName.Split(spliter,10);
string fullpath = currentpath+@"\"+filename[filename.Length-1];//生成完整的文件名
textfileup.PostedFile.SaveAs(fullpath); //保存文件
loaddir(currentpath); //重新载入当前目录
}
catch
{
lbinfo.Text="对不起,上传文件失败!";
}
{
string filePath = System.Configuration.ConfigurationSettings.AppSettinsUploadAddres"];
if(!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath += "\\"+fileName;
try
{
file.PostedFile.SaveAs(filePath);
return true;
}
catch
{
return false;
}
}
public static void DownloadFile(string FileID)
{
string FileName = DocManageBll.GetDeptFileName(FileID);
string physicalFilePath = System.Configuration.ConfigurationSettings.AppSettings["UploadAddres"]+"\\"+FileName;
if(FileName=="" || GetDeptFileShowName=="")
return ;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(GetDeptFileShowName,System.Text.Encoding.UTF8) + "\""); HttpContext.Current.Response.End();
stream.Close();
}
finally
{
}
}
我是在.net下做的c/s结构,文挡管理中的把电子文档放到数据库中
,请各位帮帮忙
{
byte[] buffer=new byte[1024];
string filename=@"c:\temp.exe";
Stream oustr=File.Create(filename);
Stream instr=response.GetResponseStream();
int i;
do
{
i=instr.Read(buffer,0,buffer.Length);
if(i>0)
oustr.Write(buffer,0,i);
}
while(i>0);
oustr.Close();
instr.Close();
System.Diagnostics.Process.Start(filename);
}
private static bool GetBinaryFile()
{
HttpWebRequest request=null;
HttpWebResponse response=null;
Stream stream=null;
StreamReader reader=null;
string URL=@"http://126.com";
try
{
Uri uri=new Uri(URL);
request=(HttpWebRequest)WebRequest.Create(uri);
response=(HttpWebResponse)request.GetResponse();
if(!request.HaveResponse)
{
return false;
}
if(response.StatusCode!=HttpStatusCode.OK)
{
return false;
}
SaveBinaryFile(response);
return true;
}
catch(Exception err)
{
return false;
}
finally
{
if(reader!=null)
reader.Close();
if(stream!=null)
stream.Close();
if(response!=null)
response.Close();
}
}