利用在服务器端的IIS,布置“请求处理映射”。从而处理,本地发出Post请求。Url指向web网站所在路径的请求映射。由映射代码实现服务器保存文件。

  winform里面使用,WebClient的对象,完成Url请求;

  

winform代码:文件保存的地址为服务器网站根目录下的files文件夹(需要提前创建)/

 OpenFileDialog fileDialog = new OpenFileDialog
{
Multiselect = false,
Title = "请选择文件",
Filter = "所有文件(*.*)|*.*"
};
if (fileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string path = Path.GetFullPath(fileDialog.FileName); //绝对路径 //显示文件路径
string fileName = Path.GetFileName(fileDialog.FileName);
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
wc.QueryString["fname"] = fileDialog.SafeFileName;
byte[] fileb = wc.UploadFile(new Uri(@"http://localhost/test.ts"), "POST", path);
string res = Encoding.GetEncoding("gb2312").GetString(fileb); //文件名上传到数据库
if (DataBaseHelper.UpLoadFileName(fileName))
{
MessageBox.Show(fileName + "上传成功");
}
else
{
MessageBox.Show(fileName + "上传失败");
} }
catch(Exception ex)
{
MessageBox.Show(ex.Message + "上传失败");
} }

目标服务器的 映射处理代码:

public void ProcessRequest(HttpContext context)
{
//在此处写入您的处理程序实现。
context.Response.ContentType = "text/plain";
try
{
HttpFileCollection files = context.Request.Files;
if (files.Count > 0)
{
files[0].SaveAs(HttpContext.Current.Server.MapPath("files/" + context.Request.QueryString["fname"]));
context.Response.Write("save success!");
}
else
context.Response.Write("hello request!");
}
catch (Exception ex)
{
context.Response.Write("save error!" + ex.Message);
}
}

客户端下载文件:

 FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < dgvContactInfo.Rows.Count; i++)
{ DataGridViewCheckBoxCell cb = (DataGridViewCheckBoxCell)this.dgvContactInfo.Rows[i].Cells[0];
bool flag = Convert.ToBoolean(cb.Value); if (flag == true)
{
try
{
string fileName = dgvContactInfo.Rows[i].Cells[1].Value.ToString(); string path = folderBrowserDialog1.SelectedPath;
WebClient wc = new WebClient();
//wc.Credentials = CredentialCache.DefaultCredentials;
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string fileUrl = @"http://localhost/files/" + fileName; wc.DownloadFile(new Uri(fileUrl), string.Format(@"{0}\{1}", path, fileUrl.Substring(fileUrl.LastIndexOf('/') + 1))); }
catch
{ }
}
}

btw:

记得修改请求限制。我就是没修改限制,导致测试的时候一直失败,以为这个方法不行。

修改根目录的Web.config 文件里面的     <httpRuntime maxRequestLength="2048000" executionTimeout="600"/> 和 iis的配置文件,可以解除上传的文件的大小限制

建议去MSDN阅读以下,关于IIS 的“模块”和“处理程序映射”文章,里面详细介绍了如何使用 映射;

从大佬的文章中窃取的代码:https://www.cnblogs.com/farmer-y/p/6179242.html

winform 利用Http向服务器上传与下载文件的更多相关文章

  1. FTP服务器上传,下载文件

    public class FtpUtil { /** * * @param host FTP服务器地址 * @param port FTP服务器端口 * @param username FTP登录账号 ...

  2. java+服务器上传和下载文件

    1.介绍enctype enctype 属性规定发送到服务器之前应该如何对表单数据进行编码. enctype作用是告知服务器请求正文的MIME类型(请求消息头content-type的作用一样) 1. ...

  3. 利用ajax与input 上传与下载文件

    html 部分代码<form action="" method="" class="form form-horizontal" nov ...

  4. 使用putty组件向服务器上传或下载文件

    基于SSH的连接 上传文件: pscp -P 28661(portNum) -pw password sourceFilePath user@serverIP:destinationFilePath ...

  5. Mac iTerm2使用lrzsz上传和下载文件

    Mac iTerm2使用lrzsz对服务器上传和下载文件 安装工具 首先需要安装iTerm2和homebrew,在终端中执行(打开终端,使用搜索(command + space),输入terminal ...

  6. 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!

    利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...

  7. 使用 PC 做 FTP/TFTP 服务器,上传和下载文件

    使用PC做TFTP服务器,上传和下载文件需要用到一个工具软件,IPOP,可百度下载. 1.在桌面新建一个空闲的文件夹,作为TFTP服务器的存储位置,然后打开IPOP软件,开启服务. 图片中 编号3 的 ...

  8. 【问题解决方案】Xshell连接服务器并实现上传和下载文件

    参考链接: Xshell连接服务器并实现上传和下载文件 第一步:xshell登录完成 略 第二步: 在服务器安装lrzsz 如果服务器的操作系统是 CentOS,则输入命令[yum install l ...

  9. SecureCRT上传和下载文件

    SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...

随机推荐

  1. EF CodeFirst系列(5)---FluentApi

    FluentApi总结 1.FluentApi简介 EF中的FluentApi作用是通过配置领域类来覆盖默认的约定.在EF中,我们通过DbModelBuilder类来使用FluentApi,它的功能比 ...

  2. MongoDB 3.6.9 集群搭建 - 切片+副本集

    1. 环境准备 在Mongo的官网下载Linux版本安装包,然后解压到对应的目录下:由于资源有限,我们采用Replica Sets + Sharding方式来配置高可用.结构图如下所示: 这里我说明下 ...

  3. 17、 利用扇贝网:https://www.shanbay.com/, 做个测单词的小工具。

    先说下,我可以说完全没有看题目要求,我只看了下扇贝网的单词测试工具就开始编码了,写出来的代码尽可能的模仿了网站上的效果. 因为把问题搞复杂了,在这个练习上耽误了很长时间,最后都不想写了,所以代码有些混 ...

  4. 【转载】 C++之split字符串分割

    https://blog.csdn.net/mary19920410/article/details/77372828

  5. [物理学与PDEs]第3章第2节 磁流体力学方程组 2.2 考虑到电磁场的存在对流体力学方程组的修正

    1.  连续性方程 $$\bex \cfrac{\p \rho}{\p t}+\Div(\rho{\bf u})=0.  \eex$$ 2.  动量守恒方程 $$\bex \cfrac{\p }{\p ...

  6. Gronwall型不等式

    Problem. Suppose $x(t)\in C[0,T]$, and satisfies $$\bex t\in [0,T]\ra 1\leq x(t)\leq C_1+C_2\int_0^t ...

  7. xls表格 ctrl+D 和ctrl+Enter区别 --快速填充相同数据,同时填充多个不同数据

    一.ctrl+Enter应用 如何快速实现下图两个图的填充值效果? ==>效果 1. 选择A列,或者所需要填充的范围 (下面利用 ctrl+G定位应用) 2.Ctrl+G 定位 选择 空值  在 ...

  8. html 超链接标签 锚点 a标签伪类

    一个简易的连接 <a href="01.html">01</a> <body> <a href="01.html" t ...

  9. codeblocks更改颜色主题

    链接:http://www.cnblogs.com/wenbosheng/p/5899483.html

  10. SQL Server - Partition by 和 Group by对比

    参考:https://www.cnblogs.com/hello-yz/p/9962356.html —————————————————— 今天大概弄懂了partition by和group by的区 ...