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

时间:2022-09-12 10:16:24 

客户端代码:


/// <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 = 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 + "<br>");
Response.Write(File.Exists(fPath + name) + "<br>");
}
}
Response.Write("上传完毕" + Directory.Exists(fPath) + Path.GetFullPath(fPath));
}
}

标签:c#,图片,服务器
0
投稿

猜你喜欢

  • 浅谈Spring与SpringMVC父子容器的关系与初始化

    2023-02-08 12:09:05
  • tk.mybatis实现uuid主键生成的示例代码

    2022-01-22 00:17:13
  • 在AOP中Spring生成代理类的两种方式

    2023-12-08 19:13:36
  • Java8中Optional类的使用说明

    2023-07-25 13:31:32
  • 详解Spring框架入门

    2023-08-14 12:56:14
  • mybatis-plus使用问题小结

    2023-10-30 06:45:58
  • C#控制台输出进度和百分比的实例代码

    2021-10-01 23:49:43
  • Spring中使用atomikos+druid实现经典分布式事务的方法

    2023-07-14 00:46:17
  • eclipse+maven+spring mvc项目基本搭建过程

    2022-12-18 03:50:52
  • 浅谈springBoot注解大全

    2023-11-09 15:02:56
  • ResultSet如何动态获取列名和值

    2022-01-16 15:54:01
  • c# base64转字符串实例

    2021-06-25 01:47:54
  • Maven工程打包jar的多种方式

    2022-12-15 06:54:46
  • Intellij Idea修改代码方法参数自动提示快捷键的操作

    2022-11-19 08:08:37
  • Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法

    2023-09-06 19:53:30
  • java中LinkedBlockingQueue与ArrayBlockingQueue的异同

    2023-08-19 02:56:49
  • Spring Boot2如何构建可部署的war包

    2023-11-29 06:40:59
  • 利用Java生成带有文字的二维码

    2022-05-21 15:01:38
  • C#中using语句的用法

    2023-07-02 05:33:24
  • SpringBoot 配置文件加密的步骤

    2023-10-23 02:55:55
  • asp之家 软件编程 m.aspxhome.com