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ù)器端圖片存儲(chǔ)的虛擬目錄名稱");//得到虛擬目錄的真實(shí)路徑//檢查存儲(chǔ)目錄
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)文章
C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器
這篇文章主要介紹了C# 使用multipart form-data方式post數(shù)據(jù)到服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)
這篇文章主要介紹了C#中Byte轉(zhuǎn)換相關(guān)的函數(shù)介紹,非常具有參考借鑒價(jià)值,特此分享到腳本之家平臺供大家學(xué)習(xí)2016-05-05c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼
開發(fā)中有時(shí)會(huì)遇到要對文件進(jìn)行共享狀態(tài)的讀寫操作,代碼如下,需要的朋友可以參考下2012-06-06分享我在工作中遇到的多線程下導(dǎo)致RCW無法釋放的問題
最近在做項(xiàng)目中遇到一個(gè)問題,在調(diào)用一個(gè)類庫中的方法時(shí),出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動(dòng)線程或其他線程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會(huì)導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能示例
這篇文章主要介紹了C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能,結(jié)合實(shí)例形式洗了C#中RichTextBox組件文字替換及改變字體顏色相關(guān)操作技巧,需要的朋友可以參考下2019-02-02