SoFunction
Updated on 2025-03-01

C# batch upload image to server example sharing

Client code:

Copy the codeThe code is as follows:

/// <summary>
/// Upload pictures in batches
/// </summary>
/// <param name="srcurl">Server path</param>
/// <param name="imagesPath">Picture folder path</param>
/// <param name="files">Image name</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;
//+  Special treatment for plus sign
if (("+"))
{
    url = srcurl + "name=" + ("+", "%2B");
}
else
{
    url = srcurl + "name=" + name;
}

FileStream fs = new FileStream(imagesPath + name, );
byte[] data = new byte[];
(data, 0, );
();

HttpWebRequest request = (HttpWebRequest)(url);
= "image/jpeg";
= "POST";
Encoding encoding = Encoding.UTF8;
= ;
Stream requestStream = ();
(data, 0, );
();


HttpWebResponse response = (HttpWebResponse)();
StreamReader streamReader = new StreamReader((), encoding);
string retString = ();
();

((count++) + "/" + );

    }
}

Server-side code:

Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

public partial class upload :
{

protected void Page_Load(object sender, EventArgs e)
{
string fPath = ("The virtual directory name of the server-side image storage");//Get the real path of the virtual directory//Check the storage directory
if (!(fPath))
{
    (fPath);
}
string name = ["name"];//get file name
(name, ("UTF-8"));

if (name != null)
{
    if (!(fPath + name))
    {
stream = ;
byte[] buffer = new byte[];
FileStream fs = null;
try
{
    fs = new FileStream(fPath + name, );
    while (((buffer, 0, )) > 0)
    {
(buffer, 0, );
    }
}
catch (IOException ioe)
{
    (ioe);
}
finally
{
    if (fs != null)
    {
();
 }
    ();
}
(name + "<br>");
((fPath + name) + "<br>");
}
}
("Uploaded" + (fPath) + (fPath));
}
}