c#批量上传图片到服务器示例分享
这篇文章主要介绍了c#批量上传图片到服务器示例,服务器端需要设置图片存储的虚拟目录,需要的朋友可以参考下
/// <summary>
/// 批量上传图片
/// </summary>
/// <param name="srcurl">服务器路径</param>
/// <param name="imagesPath">图片文件夹路径</param>
/// <param name="files">图片名称</param>
public void UpLoadFile(string srcurl, string imagesPath, List<string> files)
{
int count = ;
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, , 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, , 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, , buffer.Length)) > )
{
fs.Write(buffer, , buffer.Length);
}
}
catch (IOException ioe)
{
Response.Write(ioe);
}
finally
{
if (fs != null)
{
fs.Close();
}
stream.Close();
}
Response.Write(name + "<br>");
Response.Write(File.Exists(fPath + name) + "<br>");
}
}
Response.Write("上传完毕" + Directory.Exists(fPath) + Path.GetFullPath(fPath));
}
}
c#批量上传图片到服务器示例分享的更多相关文章
- 使用plupload绕过服务器,批量上传图片到又拍云
本文最初发布于我的个人博客:Jerry的乐园 综述 论坛或者贴吧经常会需要分享很多图片,上传图片比较差的做法是上传到中央服务器上,中央服务器再转发给静态图片服务器.而这篇文章讲介绍如何使用pluplo ...
- -Android -线程池 批量上传图片 -附php接收代码
(出处:http://www.cnblogs.com/linguanh/) 目录: 1,前序 2,类特点 3,用法 4,java代码 5,php代码 1,前序 还是源于重构,看着之前为赶时间写着的碎片 ...
- ecshop编辑器FCKeditor修改成KindEditor编辑批量上传图片
ecshop一直使用的编辑器是fck,这个不用多说,相信很多朋友用的很悲剧吧,特别是图片不能批量上传图片. 今天小编就分享一下怎么换掉fck,放上实用的kindeditor,最新ecshop版 ...
- 使用ansible批量管理远程服务器
使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...
- ASP.net(C#)批量上传图片(完整版)
摘自:http://www.biye5u.com/article/netsite/ASPNET/2010/1996.html 这篇关于ASP.Net批量上传图片的文章写得非常好,偶尔在网上看到想转 ...
- django rest framework批量上传图片及导入字段
一.项目需求 批量上传图片,然后批量导入(使用excel)每个图片对应的属性(属性共十个,即对应十个字段,其中外键三个). 二.问题 一次可能上传成百上千张图片和对应字段,原来数据库的设计我将图片和对 ...
- 使用kindeditor来替换ecshop的fckeditor编辑器,让ecshop可以批量上传图片
老杨原创 kindeditor此编辑器可以让ecshop批量上传图片,可以插入代码,可以全屏编辑,可以插入地图.视频,进行更多word操作,设置字体. 步骤一:进入kindeditor的官网,http ...
- 使用WebUploader客户端批量上传图片,后台使用springMVC接收实例
使用WebUploader客户端批量上传图片,后台使用springMVC接收实例 我是搞Java后台的,因为最近主管让用webUploader写客户端,但是在网上找了很多,能够复制就能用的并没有几个, ...
- 依赖弹出框lhdaiglog的基于WebUploader批量上传图片
初始上传界面 //链接添加弹窗 html代码段↓ var msgcontent = ""; msgcontent += '<ul class="linkAddBox ...
随机推荐
- AJAX POST请求中参数以form data和request payload形式在php中的获取方式
一.MINE TYPE问题: php对mime type为“application/x-www-form-urlencoded”(表单提交)和“multipart/form-data”(文件上传)的P ...
- java中hashSet原理
转自: http://blog.csdn.net/guoweimelon/article/details/50804799 HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1. ...
- C++之MFC基本设置
1 设置单元格的值 1) 选中指定单元格,使用SetValue设置值 CellName.Format(_T("A%d"),i);//单元格的名称 range.AttachDispa ...
- 摄像头PIN脚功能作用
摄像头PIN脚功能作用,Camera硬件系统分析 9 f E+ E2 b N. j4 M2 U- a. q9 A) T# c& O& C% x+ l5 l! q ...
- RabbitMQ学习之(三)_Centos6下RabbitMQ PHP扩展的安装
安装rabbitmq-c依赖包 yum install libtool autoconf 下载安装rabbitmq-c wget https://github.com/alanxz/rabbitmq- ...
- ERROR 1093 (HY000): You can't specify target table 'test' for update in FROM clause
MySQL执行更新语句报错: 更新语句:UPDATE test SET state=50 WHERE num IN(SELECT num FROM test WHERE state=60): 报错:E ...
- make menuconfig错误的解决办法
如果使用make menuconfig的方式配置内核,又碰巧系统没有安装ncurses库(ubuntu系统默认就没有安装此库),就会出现错误,错误信息大体上如下: *** Unable to find ...
- php7安装Memcached扩展
要安装 memcached,需要先安装依赖库 libmemcached wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/lib ...
- Oh My Fish! 让你的 Shell 漂亮起来
安装 Oh My Fish 安装 omf 很简单.你要做的只是在你的 Fish shell 中运行下面的命令. curl -L https://get.oh-my.fish | fish 一旦安装完成 ...
- linux下如何制作ext4文件系统镜像
1.生成一个空的2MiB文件 dd if=/dev/zero of=rootfs.ext4 bs=1024 count=2048 (指定每一块大小为1024字节,一共又2048块,那么就是2048 * ...