WebClient方法本身没有提供设定http请求响应超时时间的方法,我们需要重写该该类的GetWebRequest
方法,代码如下。
类名为 CGMWebClient.cs
可以根据自己的要求修改
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class CsvDataUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strResponse = "";
try
{
strResponse = UpFileData();
}
catch (Exception ex)
{
strResponse = ex.ToString();
}
Response.Write(strResponse);
// Response.Write(UPdataData());
}
/// <summary>
/// 文件上传处理
/// </summary>
/// <returns></returns>
public string UpFileData()
{
string strRet = "";
if (Request.InputStream.Length == 0)
{
//没有内容
strRet = "没有上传文件,文件长度是0。";
return strRet;
}
string strFileName = Convert.ToString(Request["fileName"]);
if (string.IsNullOrEmpty(strFileName))
{
strRet = "文件名称错误,文件上传失败。";
return strRet;
}
string strRoot = BaseComm.CommSub.GetServerPath() + "\\Updata\\";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName.Substring(0, 8) + "\\";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName;
if (System.IO.File.Exists(strRoot))
{
System.IO.File.Delete(strRoot);
}
System.IO.File.WriteAllBytes(strRoot, StreamToBytes(Request.InputStream));
return "文件【" + strFileName + "】上传完成。";
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
}
C/S端用户提交数据的测试方法方法1:
(里面有几个类需要引入,自己引入一下就可以了
)
/// <summary>
/// 上传字符长到指定服务器测试函数
/// </summary>
/// <returns></returns>
public static string UpdataStringTest()
{
//提交已知文本
//服务器上传Url地址(需要改成你自己本地的服务器地址,具体代码会在下面给出)
string strUrl = "http://www.my400800.cn";
string strRet = "";//返回值
string strFileName = "20100702_0000_0059.csv";//服务器端保存用文件名
string strUploadCvsText = @"百度
,http://www.baidu.com,192.168.0.12,2010-07-02 08:00:01
google
,http://www.google.com.hk,192.168.0.20,2010-07-02 08:00:01
400电话
,http://www.my400800.cn,192.168.0.11,2010-07-02 08:00:10
电话
,http://www.tel4006.com,192.168.0.122010-07-02 08:00:12
中国移动
,http://www.chinamobile.com,192.168.0.23,2010-07-02 08:00:30
淘宝
,http://www.taobao.com,192.168.0.81,2010-07-02 08:00:60"; //需要上传的文本内容
CGMWebClient wc = new CGMWebClient();
wc.Timeout = 10 * 60;//10分钟
//用默认编码转换网页内容
Encoding encoding = Encoding.GetEncoding("utf-8");
if (strUploadCvsText.Length > 10)//文件长度小于10时不上传文件
{
byte[] buffer = wc.UploadData(strUrl + "?fileName=" + strFileName, encoding.GetBytes(strUploadCvsText));
strRet = encoding.GetString(buffer);
strRet = strRet.Replace(" ", "").Replace("\r\n", "");
}
else
{
strRet = "【" + strFileName + "】没有可上传数据。";
}
return strRet;
}
C/S端用户提交数据的测试方法方法2:
(里面有几个类需要引入,自己引入一下就可以了
)
/// <summary>
/// 上传指定文件到Web服务器测试函数
/// </summary>
/// <returns></returns>
public static string UpdataFileWebServerTest()
{
//提交已知文本
//服务器上传Url地址(需要改成你自己本地的服务器地址,具体代码会在下面给出)
string strUrl = "http://www.my400800.cn";
string strRet = "";//返回值
string strFileName = "20100702_0000_0059.rar";//服务器端保存用文件名
string strUpFilePath = "E:/test.rar";//你本地文件存放路径,需要根据自己的需求修改
CGMWebClient wc = new CGMWebClient();
wc.Timeout = 10 * 60;//10分钟
if (!System.IO.File.Exists(strUpFilePath))//文件长度小于10时不上传文件
{
//用默认编码转换网页内容
Encoding encoding = Encoding.GetEncoding("utf-8");
//文件读取
byte[] readFileByteArr = System.IO.File.ReadAllBytes("strUpFilePath");
byte[] buffer = wc.UploadData(strUrl + "?fileName=" + strFileName, readFileByteArr);
strRet = encoding.GetString(buffer);
strRet = strRet.Replace(" ", "").Replace("\r\n", "");
}
else
{
strRet = "【" + strFileName + "】没有可上传数据。";
}
return strRet;
}
web端代码(iis6.0,asp.net 2.0)
CsvDataUpload.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class CsvDataUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strResponse = "";
try
{
strResponse = UpFileData();
}
catch (Exception ex)
{
strResponse = ex.ToString();
}
Response.Write(strResponse);
// Response.Write(UPdataData());
}
/// <summary>
/// 文件上传处理
/// </summary>
/// <returns></returns>
public string UpFileData()
{
string strRet = "";
if (Request.InputStream.Length == 0)
{
//没有内容
strRet = "没有上传文件,文件长度是0。";
return strRet;
}
string strFileName = Convert.ToString(Request["fileName"]);
if (string.IsNullOrEmpty(strFileName))
{
strRet = "文件名称错误,文件上传失败。";
return strRet;
}
string strRoot = BaseComm.CommSub.GetServerPath() + "\\Updata\\";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName.Substring(0, 8) + "\\";
if (!System.IO.Directory.Exists(strRoot))
{
System.IO.Directory.CreateDirectory(strRoot);
}
strRoot += strFileName;
if (System.IO.File.Exists(strRoot))
{
System.IO.File.Delete(strRoot);
}
System.IO.File.WriteAllBytes(strRoot, StreamToBytes(Request.InputStream));
return "文件【" + strFileName + "】上传完成。";
}
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
}
CsvDataUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CsvDataUpload.aspx.cs" Inherits="CsvDataUpload" %>
分享到:
相关推荐
C# WebClient 上传文件 WebClient上传文件至服务器
本篇文章主要介绍了C#中WebClient实现文件下载,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本程序就主要是通过C# 中的WebClient 类实现对文件的下载功能,其中涉及到进度条,对下载成功与下载失败都会给出相应的提示,本程序是通过VS 2005 开发的,希望给需要在项目中用到下载这个功能的人有所帮助!...
C# WEBClient 文件上传下载
下载开箱即用:C#使用WebClient获取网页源文件例子:该例子演示了通过WebClient获取百度首页源文件,并进行UTF8解码,存入一个字符串中。
c# 实现文件FTP上传至服务器 WebClient上传文件至服务器(不带进度条) 要上传的文件(全路径格式) <param name="strUrlDirPath">Web服务器文件夹路径 <returns>True/False是否上传成功</returns>
本代码通过webclient下载,下载文件支持回调显示进度等。
该资源使用Webclient 类后台自动获取网络上指定的文件内容,下载过程中可以显示实时下载速度和进度,下载完成之后又提示信息。
使用WebClient类和FileStream类实现文件下载
通过向任一服务器发送http请求,查看server端过来的内容,状态行,响应报头,空行,实际内容. 这可以帮助理解http协议. 运行方法: 1.有eclipse或其它开发环境下: 将里面的java源文件放入任一java工程的default包中, ...
C#中使用WebClient下载指定url的网络照片示例源码--VS2013
主要介绍了C#中在WebClient中使用post发送数据实现方法,需要的朋友可以参考下
利用WebClient类向服务器上载文件
WebClient下载文件展示进度条
那么WebClient将会报内存不足异常(Out of Memory Exceptions),究其原因是因为WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器。本文将讲述如何采用HttpWebRequest方式每次...
例如,当您从响应很慢的API服务器下载大文件时,您的应用程序的UI不会停止响应。HttpWebRequest通常和WebResponse一起使用,一个发送请求,一个获取数据。HttpWebRquest更为底层一些,能够对整个访问过程有个直观的...
C#的Winform开发的下载器,下载文件夹,带速度计算和文件下载计算。
C#中的WebClient类编写整站下载软件
博文链接:https://roger51.iteye.com/blog/123754
airdcpp-webclient, 文件服务器/nas设备的公共 点对点 文件共享应用程序 AirDC 网络客户端AirDC Web客户机是本地安装的应用程序,它用于在本地网络或者网络中经常共享文件或者目录。 可以在不同类型的系统上安装守护...