c#a服务器上传文件b服务器,C#_c#批量上传图片到服务器示例分享,客户端代码: 复制代码 代码 - phpStudy...

c#批量上传图片到服务器示例分享

客户端代码:

///

/// 批量上传图片

///

/// 服务器路径

/// 图片文件夹路径

/// 图片名称

public void UpLoadFile(string srcurl, string imagesPath, List files)

{

int count = 1;

foreach (string imageName in files)

{

string name = imageName;

string url = null;

//+  加号特殊处理

if (name.Contains("+"))

{

url = srcurl + "name=" + name.Replace("+", "%2B");

}

else

{

url = srcurl + "name=" + name;

}

FileStream fs = new FileStream(imagesPath + name, FileMode.Open);

byte[] data = new byte[fs.Length];

fs.Read(data, 0, data.Length);

fs.Close();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

request.ContentType = "image/jpeg";

request.Method = "POST";

Encoding encoding = Encoding.UTF8;

request.ContentLength = data.Length;

Stream requestStream = request.GetRequestStream();

requestStream.Write(data, 0, data.Length);

requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader streamReader = new StreamReader(response.GetResponseStream(), encoding);

string retString = streamReader.ReadToEnd();

streamReader.Close();

Console.WriteLine((count++) + "/" + files.Count);

}

}

服务器端代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;

using System.Text;

using System.IO;

public partial class upload : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string fPath = Server.MapPath("服务器端图片存储的虚拟目录名称");//得到虚拟目录的真实路径//检查存储目录

if (!Directory.Exists(fPath))

{

Directory.CreateDirectory(fPath);

}

string name = Request.QueryString["name"];//得到文件名

HttpUtility.UrlEncode(name, Encoding.GetEncoding("UTF-8"));

if (name != null)

{

if (!File.Exists(fPath + name))

{

System.IO.Stream stream = Request.InputStream;

byte[] buffer = new byte[stream.Length];

FileStream fs = null;

try

{

fs = new FileStream(fPath + name, FileMode.Create);

while ((stream.Read(buffer, 0, buffer.Length)) > 0)

{

fs.Write(buffer, 0, buffer.Length);

}

}

catch (IOException ioe)

{

Response.Write(ioe);

}

finally

{

if (fs != null)

{

fs.Close();

}

stream.Close();

}

Response.Write(name + "
");

Response.Write(File.Exists(fPath + name) + "
");

}

}

Response.Write("上传完毕" + Directory.Exists(fPath) + Path.GetFullPath(fPath));

}

}相关阅读:

MongoDB快速入门笔记(一)之windows下安装MongoDB方法

Win10系统查看补丁是否更新到最新的方法

javascript实现校验文件上传控件实例

PHP缓冲区用法总结

jQuery实现下拉加载功能实例代码

数组与类使用PHP的可变变量名需要的注意的问题

C语言实现双向链表

JavaScript中的原型和继承详解(图文)

oracle数据库索引失效

php配置php-fpm启动参数及配置详解

Duang!苹果电脑mac OS X五款视频播放器详细评测图解

Android Toast通知用法实例详解

java数据结构与算法之noDups去除重复项算法示例

JS实现倒计时和文字滚动的效果实例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值