日期:2014-05-18  浏览次数:20476 次

C# 写 ASP。net 怎么实现上传下载啊???新手学习
C#   写   ASP。net   怎么实现上传下载啊???新手学习

各位高手帮忙,说具体点谢谢,
新手上路~   第一次弄B/S  
给点具体代码看看~~

------解决方案--------------------
private void btnUpload_ServerClick(object sender, System.EventArgs e)
{
string f = this.myFile.PostedFile.FileName;
f = f.Substring(f.LastIndexOf( "\\ ")+1);
this.myFile.PostedFile.SaveAs(Server.MapPath( "zhu ")+ "\\ "+f);
}

private void DownLoad(string fname)
{
FileStream fs = new FileStream(Server.MapPath( "zhu ") + "\\ "+fname,FileMode.Open);
long fsize = fs.Length;
Context.Response.ContentType = "application/octet-stream ";
Context.Response.AddHeader( "Content-Disposition ", "attachment; filename=\ " "
+ HttpUtility.UrlEncode(fname,System.Text.Encoding.UTF8) + "\ " ");
Context.Response.AddHeader( "Content-Length ",fsize.ToString());
byte[] fileBuffer = new byte[fsize];
fs.Read(fileBuffer, 0, (int)fsize);
fs.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
}

private void btnDown_ServerClick(object sender, System.EventArgs e)
{
this.DownLoad(this.downFile.Value);
}


------解决方案--------------------
单文件上传的cs代码:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName == " ")
{
Label1.Text = "要上传的文件不允许为空! ";
return;
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
string serverpath = Server.MapPath( "File/ ") + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
Label1.Text = "上传成功! ";
}
}
catch (Exception error)
{
Label1.Text = "处理发生错误!原因: " + error.ToString();
}
}
}