webclient实现多图片上传
来源:互联网 发布:科立捷对讲机端口是几 编辑:程序博客网 时间:2024/04/27 21:34
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ar = Server.MapPath("~/Image")+"/";
}
private static string ar = "";
protected void Button1_Click(object sender, EventArgs e)
{
try
{
Invoker ins = new Invoker();
ins.Invoke();
ins.InvokeMethod();
Response.Write("成功");
}
catch
{ }
}
protected void Button2_Click(object sender, EventArgs e)
{
}
public class Invoker
{
public OpenFileDialog InvokeDialog;
private Thread InvokeThread;
private DialogResult InvokeResult;
public Invoker()
{
InvokeDialog = new OpenFileDialog();
InvokeDialog.Multiselect = true;
InvokeDialog.Filter = "图片(*.gif;*.jpg;*.jpeg;*.jnp;*.bmp)|*.gif;*.jpg;*.jpeg;*.jnp;*.bmp|所有文件(*.*)|*.*";
InvokeThread = new Thread(new ThreadStart(InvokeMethod));
InvokeThread.SetApartmentState(ApartmentState.STA);
InvokeResult = DialogResult.None;
}
public DialogResult Invoke()
{
InvokeThread.Start();
InvokeThread.Join();
return InvokeResult;
}
public void InvokeMethod()
{
InvokeResult = InvokeDialog.ShowDialog();
foreach (string str in InvokeDialog.FileNames)
{
UpLoadFile1(str, ar);
}
}
}
/// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
private static void UpLoadFile1(string fileNamePath, string uriString)
{
try
{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//") + 1);
string Name = fileName.Substring(0, fileName.LastIndexOf("."));
string NewFileName = Name+""+ fileNamePath.Substring(fileNamePath.LastIndexOf("."));
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (fileNameExt == "gif" || fileNameExt == "jpg" || fileNameExt == "jpeg" || fileNameExt == "jnp" || fileNameExt == "bmp" || fileNameExt == "GIF" || fileNameExt == "JPG" || fileNameExt == "JPEG" || fileNameExt == "JNP" || fileNameExt == "BMP")
{
if (uriString.EndsWith("/") == false)
{
uriString = uriString + "/";
}
uriString = uriString + NewFileName;
// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
byte[] postArray = r.ReadBytes((int)fs.Length);
int i = postArray.Length;
if (i / 1024 < 512)
{
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
}
postStream.Close();
}
else
{
}
}
catch
{
}
}
else
{
}
}
catch
{
}
}
}
- webclient实现多图片上传
- C# WebClient 多文件上传实现
- Silverlight WebClient 上传实现
- 使用WebClient实现文件上传
- 多图片上传实现
- 在Silverlight 2 (Beta2) 中使用webclient上传图片
- .net 文件下载上传、网页抓取 用WebClient实现
- .net 文件下载上传、网页抓取 用WebClient实现
- WebClient实现通讯(一)(上传和下载字符串数据)
- WebClient 轻松实现文件下载上传、网页抓取
- C# 实现多图片上传
- js实现多图片上传
- php 实现多图片上传
- PHP实现多图片上传
- PHP实现多图片上传
- KindEditor实现多图片上传
- webclient上传文件
- 利用WebClient上传文件
- 利用栈和循环队列判断回文
- 灰度图像淡入到彩色图像与彩色图像淡出到灰度图像
- 决策树的构造——一个手工例子
- HR相关表
- VC++深入详解 孙鑫作品系列
- webclient实现多图片上传
- NHibernate教程
- 尘埃落定,长出一口气。。。。
- DBGridEH 详细参数
- c++的基本程序a
- VB.net Winform多国语言的实现
- 关于ORACLE数据库中权限和角色的探索
- 360buy的服务器返回的一个错误
- C++基本程序b