c#批量上傳圖片到服務(wù)器示例分享
客戶端代碼:
/// <summary>
/// 批量上傳圖片
/// </summary>
/// <param name="srcurl">服務(wù)器路徑</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);
}
}
服務(wù)器端代碼:
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("服務(wù)器端圖片存儲的虛擬目錄名稱");//得到虛擬目錄的真實(shí)路徑//檢查存儲目錄
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));
}
}
相關(guān)文章
Unity中的PostProcessScene實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity中的PostProcessScene實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
利用WPF實(shí)現(xiàn)Windows屏保的制作
屏保程序的本質(zhì)上就是一個Win32?窗口應(yīng)用程序。本文將利用WPF實(shí)現(xiàn)Windows屏保的制作,文中的示例代碼簡潔易懂,對我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下2022-07-07
vista和win7在windows服務(wù)中交互桌面權(quán)限問題解決方法:穿透Session 0 隔離
服務(wù)(Service)對于大家來說一定不會陌生,它是Windows 操作系統(tǒng)重要的組成部分。我們可以把服務(wù)想像成一種特殊的應(yīng)用程序,它隨系統(tǒng)的“開啟~關(guān)閉”而“開始~停止”其工作內(nèi)容,在這期間無需任何用戶參與2016-04-04
C#使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成
這篇文章主要為大家詳細(xì)介紹了C#如何使用PPT組件的CreateVideo方法實(shí)現(xiàn)視頻生成,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10

