winform 上传文件
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 上传文件的更多相关文章
- winform上传文件
//上传图片 文件 public int addUpPic( String strProCode,String strFileName,String strUpType) { //strFileNam ...
- WinForm上传文件,下载文件
上传文件: 使用OpenFileDialog控件选择文件, 具体代码示例: private void btnUpLoadPic_Click(object sender, EventArgs e) { ...
- winform上传文件,利用http,form-data格式上传
/// <summary> /// 上传文件 /// </summary> /// <param name="url">服务地址</par ...
- C# winform 上传文件到服务器
1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. 声明:这个方法虽然最简单最省事,但是上传大文件可能会报错,我的机器是10M, 超过10M就会提示报 ...
- winform上传文件到服务器——资料整理
标题:使用简单的wcf文件实现上传,下载文件到服务器 地址:https://blog.csdn.net/duanzi_peng/article/details/19037777
- c# Winform上传文件
http://blog.csdn.net/shihuan10430049/article/details/3734398这个代码有点问题 http://blogs.msdn.com/b/johan/a ...
- 客户端(Winform窗体)上传文件到服务器(web窗体)简单例子
客户端:先创建一个winform窗体的应用程序项目 项目结构
- Winform上传下载文件代码
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- C#在WinForm下使用HttpWebRequest上传文件
转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...
随机推荐
- MFC 视图、文档、框架(通讯)
CMainFrame * pMainWnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;//主框架 CChildFrame * pChild = (CChildFr ...
- Rstudio编辑界面美化设置
美化Rstudio的编辑界面有利于我们输入代码,合适的调整更是减少错误. 可以根据自己的喜好和习惯选择.
- 怎么修改路由器地址的默认IP
参考文章:http://jingyan.baidu.com/article/4b52d7026e14effc5c774b30.html 一.怎么修改路由器地址的默认IP 目前绝大多数品牌有线或无线路 ...
- BZOJ 4236: JOIOJI MAP
4236: JOIOJI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.ph ...
- iOS开发——语法&高级Block练习
高级Block练习 一 .最简单的block使用 使用block的三个步骤:1.定义block变量 2.创建block代码块 3.调用block匿名函数 定义一个block的构成包括:返回值,bloc ...
- PHP: 深入pack/unpack 字节序
http://my.oschina.net/goal/blog/195749?p=1 目录[-] 写在前面的话 什么是字节序 MSB和LSB 大端序 小端序 网络字节序 主机字节序 总结 pack/u ...
- IDEA 编译错误:java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)
错误描述 在用IDEA编译别人的项目的时候遇到下面的错误: java: try-with-resources is not supported in -source 1.6 (use -source ...
- jsp自定义标签分页
第一步:建立分页实体page类 package com.soda.util; /** * @description 分页实体类 * @author line * @time 2016年8月28日11: ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- xcode笔记
1.Alt键的使用 2.设置捕捉所有意外断点:停在代码出错处 2015年07月27日09:52:12 3.搜索 command + F:在当前的文件中搜索 command + Shift ...