求能控制上传文件大小的asp.net程序 作用和fileupload功能一样,但是fileupload要在上传完毕后才能知道文件大小。也就是说我想要txt说明文件的时候对方给我一个高清电影我也要先收下。请问有没有方法使得文件大于预订限制就中断上传? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 swf上传、Silverlight上传都能实现。你搜寻下,很多这样的代码的 上传断点续传using System;using System.Collections.Generic;using System.Text;using System.Collections.Specialized;using System.Net;using System.Net.Sockets;using System.IO;namespace CompleteClient{ /// <summary> /// 文件发送工作类 /// </summary> class PostFile { /// <summary> /// 通过post发送指定文件的指定字段到指定的uri上 /// </summary> /// <param name="uploadfile">上传文件路径</param> /// <param name="url">上传的到的URi位置</param> /// <param name="offset">当前偏移量</param> /// <param name="size">需要发送的块大小</param> /// <param name="fileFormName">服务器端"GET"取得的文件名</param> /// <param name="contenttype">文件类型(保留用)</param> /// <param name="querystring">GET数组(供服务器用GET取得一些信息)</param> /// <param name="cookies">本地cookies(保留用)</param> /// <returns>uri的response的内容以string的形式返回</returns> public string UploadFileEx(string uploadfile, string url, long offset, long size, string fileFormName, string contenttype, NameValueCollection querystring, CookieContainer cookies) { if ((fileFormName == null) || (fileFormName.Length == 0)) { fileFormName = "file"; } if ((contenttype == null) || (contenttype.Length == 0)) { contenttype = "application/octet-stream"; } string postdata; postdata = "?"; if (querystring != null) { foreach (string key in querystring.Keys) { postdata += key + "=" + querystring.Get(key) + "&"; } } Uri uri = new Uri(url + postdata); string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); webrequest.CookieContainer = cookies; webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; // 构造一个post请求的http头 StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); sb.Append("\"; filename=\""); sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(contenttype); sb.Append("\r\n"); sb.Append("\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); // Build the trailing boundary string as a byte array // ensuring the boundary appears on a line by itself byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + (long)size + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream(); // 写入post头 requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 写入文件内容 byte[] buffer = new Byte[size]; fileStream.Seek(offset, SeekOrigin.Current); fileStream.Read(buffer, 0, buffer.Length); requestStream.Write(buffer, 0, buffer.Length); // 写入post请求的尾 requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); //读取服务器的反馈消息 WebResponse responce = webrequest.GetResponse(); Stream s = responce.GetResponseStream(); StreamReader sr = new StreamReader(s); return sr.ReadToEnd(); } }} uploadify和swfupload都可以用,很简单 求用代码设置GridView的PagerTemplate C#远程登录hotmail 关于SQL过滤条件的定制。。 在gridview里怎么区分两个选择按钮 寻找支持Asp.Net 2.0的商业空间 在web.config中怎么获得当前路径,或者在一个.cs文件中获得当前路径 在asp.net中 如何把HTML代码保存到数据库里面? 上传文件到数据库出错???vb 关于Attributes.Add()问题 asp.net 打印 问题 Button点击无效 我需要定义一个checked变量,但checked是C#关键字,有没有办法解决
断点续传
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace CompleteClient
{
/// <summary>
/// 文件发送工作类
/// </summary>
class PostFile
{
/// <summary>
/// 通过post发送指定文件的指定字段到指定的uri上
/// </summary>
/// <param name="uploadfile">上传文件路径</param>
/// <param name="url">上传的到的URi位置</param>
/// <param name="offset">当前偏移量</param>
/// <param name="size">需要发送的块大小</param>
/// <param name="fileFormName">服务器端"GET"取得的文件名</param>
/// <param name="contenttype">文件类型(保留用)</param>
/// <param name="querystring">GET数组(供服务器用GET取得一些信息)</param>
/// <param name="cookies">本地cookies(保留用)</param>
/// <returns>uri的response的内容以string的形式返回</returns>
public string UploadFileEx(string uploadfile, string url, long offset, long size,
string fileFormName, string contenttype,
NameValueCollection querystring, CookieContainer cookies)
{
if ((fileFormName == null) ||
(fileFormName.Length == 0))
{
fileFormName = "file";
} if ((contenttype == null) ||
(contenttype.Length == 0))
{
contenttype = "application/octet-stream";
} string postdata;
postdata = "?";
if (querystring != null)
{
foreach (string key in querystring.Keys)
{
postdata += key + "=" + querystring.Get(key) + "&";
}
}
Uri uri = new Uri(url + postdata); string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.CookieContainer = cookies;
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST"; // 构造一个post请求的http头
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(fileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(uploadfile));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append(contenttype);
sb.Append("\r\n");
sb.Append("\r\n"); string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); // Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes =
Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); FileStream fileStream = new FileStream(uploadfile,
FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + (long)size +
boundaryBytes.Length;
webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream();
// 写入post头
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); // 写入文件内容
byte[] buffer = new Byte[size];
fileStream.Seek(offset, SeekOrigin.Current);
fileStream.Read(buffer, 0, buffer.Length); requestStream.Write(buffer, 0, buffer.Length); // 写入post请求的尾
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
//读取服务器的反馈消息
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
StreamReader sr = new StreamReader(s); return sr.ReadToEnd();
}
}
}