• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#批量上传图片到服务器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

客户端代码:

 /// <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));
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【C语言】用C语言输出一个吃豆人发布时间:2022-07-14
下一篇:
C#利用ICSharpCode将远程文件打包并下载发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap