logo
VitNX3 Documentation

# SendDataToSites.FileUploader Method

Namespace: VitNX3.Functions.Web
Assembly: VitNX3 (in VitNX3.dll)
Starts the uploading. Example:https://gist.github.com/Zalexanninev15/13df3d870ee9951b65081212cde9863a
VB
C#
F#
C++
JScript
Copy

    public static void FileUploader(
       string url,
       NameValueCollection values,
       string filePath,
       Action<string, int> progress,
       Action<string> completed
    )

    This language is not supported or no code example is available.

#### Parameters

url
  string

The url.

values
  NameValueCollection

The values.

filePath
  string

The file path.

progress
  Action<string, int>

The progress.

completed
  Action<string>

The completed.

Source code
VB
C#
F#
C++
JScript
Copy

    public static void FileUploader(string url,
         NameValueCollection values,
         string filePath,
         Action<string, int> progress,
         Action<string> completed)
     {
         var fileStream = File.OpenRead(filePath);
         var fileName = Path.GetFileName(filePath);
         var ms = new MemoryStream();
         fileStream.CopyTo(ms);
         ms.Position = 0;
         try
         {
             const string contentType = "application/octet-stream";
             var request = WebRequest.Create(url);
             request.Method = "POST";
             var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
             request.ContentType = "multipart/form-data; boundary=" + boundary;
             boundary = "--" + boundary;
             var dataStream = new MemoryStream();
             byte[] buffer;
             foreach (string name in values.Keys)
             {
                 buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                 dataStream.Write(buffer, 0, buffer.Length);
                 buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}",
                     name, Environment.NewLine));
                 dataStream.Write(buffer, 0, buffer.Length);
                 buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                 dataStream.Write(buffer, 0, buffer.Length);
             }
             buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
             dataStream.Write(buffer, 0, buffer.Length);
             buffer = Encoding.UTF8.GetBytes($"Content-Disposition: form-data; name=\"file\"; filename=\"{fileName}\"{Environment.NewLine}");
             dataStream.Write(buffer, 0, buffer.Length);
             buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}",
                 contentType,
                 Environment.NewLine));
             dataStream.Write(buffer, 0, buffer.Length);
             ms.CopyTo(dataStream);
             buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
             dataStream.Write(buffer, 0, buffer.Length);
             buffer = Encoding.ASCII.GetBytes(boundary + "--");
             dataStream.Write(buffer, 0, buffer.Length);
             dataStream.Position = 0;
             request.ContentLength = dataStream.Length;
             var requestStream = request.GetRequestStream();
             var size = dataStream.Length;
             const int chunkSize = 64 * 1024;
             buffer = new byte[chunkSize];
             long bytesSent = 0;
             int readBytes;
             while ((readBytes = dataStream.Read(buffer, 0, buffer.Length)) > 0)
             {
                 requestStream.Write(buffer, 0, readBytes);
                 bytesSent += readBytes;
                 var status = "Uploading... " + bytesSent / 1024 + "KB of " + size / 1024 + "KB";
                 var percentage = Convert.ToInt32(100 * bytesSent / size);
                 progress(status, percentage);
             }
             using (var response = request.GetResponse())
             using (var responseStream = response.GetResponseStream())
             using (var stream = new MemoryStream())
             {
                 responseStream.CopyTo(stream);
                 var result = Encoding.Default.GetString(stream.ToArray());
                 completed(result == string.Empty
                     ? "failed:" + result
                     : "ok:" + result);
             }
         }
         catch (Exception e) { completed(e.ToString()); }
     }

    This language is not supported or no code example is available.

Applies to

#### .NET Framework

Supported in: 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

#### .NET Core

Supported in: 5.0+, 6.0+
See Also
SendDataToSites Class
VitNX3.Functions.Web Namespace