C# WinForm 文件上传下载
C# WinForm 文件上传下载收藏
山东特菜/**//**//**//// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="localFilePath">文件名,全路径格式</param>
/// <param name="serverFolder">服务器文件夹路径</param>
/// <param name="reName">是否需要修改文件名,这里默认是日期格式</param>
/// <returns></returns>
public static bool UploadFile(string localFilePath, string serverFolder,bool reName)
......{
string fileNameExt, newFileName, uriString;
if (reName)
......{
fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
newFileName = DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
}
else
......{
newFileName = localFilePath.Substring(localFilePath.LastIndexOf("")+1);
}
if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith(""))
......{
serverFolder = serverFolder + "/";
}
uriString = serverFolder + newFileName; //服务器保存路径
/**//**//**//// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
......{
/
/使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
......{
postStream.Write(postArray, 0, postArray.Length);
}
else
......{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
......{
//MessageBox.Show("文件上传失败,请稍候重试~");
return false;
}
return true;
}
/**//**//**//// <summary>
/
// 下载服务器文件至客户端
/// </summary>
/// <param name="uri">被下载的文件地址</param>
/// <param name="savePath">另存放的目录</param>
public static bool Download(string uri, string savePath) ......{
string fileName; //被下载的文件名
if (uri.IndexOf("") > -1)
......{
fileName = uri.Substring(uri.LastIndexOf("") + 1);
}
else
.
.....{
fileName = uri.Substring(uri.LastIndexOf("/") + 1);
}
if (!savePath.EndsWith("/") && !savePath.EndsWith(""))
......{
savePath = savePath + "/";
}
savePath += fileName; //另存为的绝对路径+文件名
WebClient client = new WebClient();
try
......{
client.DownloadFile(uri, savePath);
}
catch
......{
return false;
}
return true;
欹}
************************************************
命名空间
System.Net;
System.IO;
上传IIS虚拟目录需要给写入权限,下载可能需要匿名访问权限。
本文来自CSDN博客,转载请标明出处:blog.csdn/wxg22526451/archive/2008/03/24/2213874.aspx
字号:大中小
using System;
using System.Web;
using System.IO;
namespace leo
{
/// <summary>
/// 文件上传类
/// 作者:leo
/// 制作时间:2006-2-13 最后修改于2007-6-25
/// </summary>
public class UploadFile
{
public UploadFile(HttpPostedFile PostedFile)
中国排名前十奶粉品牌
{
FileObj = PostedFile;
}
private HttpPostedFile FileObj;
private int maxSize = 150 * 1024; //默认150KB
private string filesType = ".jpeg|.jpg|.gif"; //默认文件类型
private bool oldFileName = false; //是否采用原文件名private string filesPath = ""; //设置文件夹路径x战警系列顺序
private string Eror = ""; //错误信息
private int errorNum = 0; //错误码
private string savaFileName = ""; //上传成功后的文件名称
高贵的施舍读后感//属性区
public int MaxSizeKB
{
get{ return maxSize / 1024; }
set{ maxSize = value * 1024;}
核定征收
}
public string FilesType
{
get{ return filesType; }
set{ filesType = value.ToLower();} }
public string FilesPath
{
get{ return filesPath; }
set{ filesPath = value;}
}
public string SavaFileName
{
get{ return savaFileName; }
set{ savaFileName = value;}
}
public string SaveEror
{
get{ return Eror; }
}
public int ErrorNum
{
get { return errorNum; } }
public bool OldFileName
{
get{ return oldFileName; }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。