using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO; namespace Common
{
/// <summary>
/// winform形式的文件传输类
/// </summary>
public class WinFileTransporter
{
/// <summary>
/// WebClient上传文件至服务器,默认不自动改名
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
public void UpLoadFile(string fileNamePath, string uriString)
{
UpLoadFile(fileNamePath, uriString, false);
}
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
/// <param name="IsAutoRename">是否自动按照时间重命名</param>
public void UpLoadFile(string fileNamePath, string uriString, bool IsAutoRename)
{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
string NewFileName = fileName;
if (IsAutoRename)
{
NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
} string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (uriString.EndsWith("/") == false) uriString = uriString + "/"; uriString = uriString + NewFileName;
Utility.LogWriter log = new Utility.LogWriter();
//log.AddLog(uriString, "Log");
//log.AddLog(fileNamePath, "Log");
/**/
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT"); try
{ //使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath); if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
postStream.Close();
fs.Dispose();
log.AddLog("上传日志文件成功!", "Log");
}
else
{
postStream.Close();
fs.Dispose();
log.AddLog("上传日志文件失败,文件不可写!", "Log");
} }
catch (Exception err)
{
postStream.Close();
fs.Dispose();
//Utility.LogWriter log = new Utility.LogWriter();
log.AddLog(err, "上传日志文件异常!", "Log");
throw err;
}
finally
{
postStream.Close();
fs.Dispose();
}
} /**/
/// <summary>
/// 下载服务器文件至客户端 /// </summary>
/// <param name="URL">被下载的文件地址,绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public void Download(string URL, string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名 string Path = Dir + fileName; //另存为的绝对路径+文件名
Utility.LogWriter log = new Utility.LogWriter();
try
{
WebRequest myre = WebRequest.Create(URL);
}
catch (Exception err)
{
//MessageBox.Show(exp.Message,"Error");
log.AddLog(err, "下载日志文件异常!", "Log");
} try
{
client.DownloadFile(URL, fileName);
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] mbyte = r.ReadBytes((int)fs.Length); FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, (int)fs.Length);
fstr.Close(); }
catch (Exception err)
{
//MessageBox.Show(exp.Message,"Error");
log.AddLog(err, "下载日志文件异常!", "Log");
}
} }
}

上网找的时候还有为朋友提供了winform上传的另一种解决方案,确实很有意思,也记录如下
http://www.cnblogs.com/njnudt/archive/2007/08/08/847324.html

winform 上传文件的更多相关文章

  1. winform上传文件

    //上传图片 文件 public int addUpPic( String strProCode,String strFileName,String strUpType) { //strFileNam ...

  2. WinForm上传文件,下载文件

    上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...

  3. winform上传文件,利用http,form-data格式上传

    /// <summary> /// 上传文件 /// </summary> /// <param name="url">服务地址</par ...

  4. C# winform 上传文件到服务器

    1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. 声明:这个方法虽然最简单最省事,但是上传大文件可能会报错,我的机器是10M, 超过10M就会提示报 ...

  5. winform上传文件到服务器——资料整理

    标题:使用简单的wcf文件实现上传,下载文件到服务器 地址:https://blog.csdn.net/duanzi_peng/article/details/19037777

  6. c# Winform上传文件

    http://blog.csdn.net/shihuan10430049/article/details/3734398这个代码有点问题 http://blogs.msdn.com/b/johan/a ...

  7. 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子

    客户端:先创建一个winform窗体的应用程序项目 项目结构

  8. Winform上传下载文件代码

    using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...

  9. C#在WinForm下使用HttpWebRequest上传文件

    转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...

随机推荐

  1. 【Java】Socket+多线程实现控制台聊天室

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827212.html 聊天室程序的结构图: 架构解释: Server服务器相当于一个中转站,Client客户端 ...

  2. mysql数据库表间内外链接详解

    1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)(2)右外连接(右边的表不加限制)(3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语法: ...

  3. 编码规范系列(一):Eclipse Code Templates设置

    从工作开始,经历了几个项目的开发,现在的项目一般都是一个团队共同开发,而每个人都有自己的编码习惯,为了统一格式,项目组在项目开发之前都会制定一系列的规范.俗话说约定优于配置,但是在执行过程中往往发现效 ...

  4. 【M6】区别increment/decrement操作符的前置(prefix)和后置(postfix)形式

    1.考虑++(--的情况是一样的),前置是累加然后取出,后置是取出然后累加. 2.重载方法根据形参表的不同区分,问题来了,前置和后置形式都没有形参,因此没法区分.怎么办? 对于后置增加一个形参int, ...

  5. 2015南阳CCPC A - Secrete Master Plan 水题

    D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Master Mind KongMing gave ...

  6. Codeforces Gym 100513D D. Data Center 前缀和 排序

    D. Data Center Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560/proble ...

  7. delphi 查找对话框

    调用查找对话框 关键点 HTMLID_FIND = 1; //查找对话框 HTMLID_VIEWSOURCE= 2; //用记事本查看源代码 HTMLID_OPTIONS =3; //Internet ...

  8. Tomcat7集群扩展session集中管理,tomcat-redis-session-manager使用

    请参考官方文档 下载所需的包了: tomcat-redis-session-manager-1.1.jar jedis-2.1.0.jar commons-pool-1.6.jar 将这些jar包都丢 ...

  9. [Angular 2] Import custom module

    The application structure: in app.module.ts: import { NgModule} from "@angular/core"; impo ...

  10. redo log write和flush

    http://bbs.chinaunix.net/thread-1753130-1-1.html 在事务提交时innobase会调用ha_innodb.cc 中的innobase_commit,而in ...