Hi All,
I am trying to upload multiple files to blob storage at a single time in an file array to blob storage using API in uipath.(client requirement).
But I am facing issue with the file array to upload.
I am able to connect with the blob container but unable to upload multiple files at a time. For example I am having 3 files in a folder I am using collection to store all the files. So now I have to attach the collection in the API to upload the files in the blob container.
while attaching I am unable to do it because there is no type called collection in the propeties panel near attachments in the http request activity.
I had got a solution of myself through the c# code. below is the code
Need to add few dll’s before using the code
system.net.http
system.net.http.headers
system.text.regularexpressions
system.net.xml
system.net.xml.linq
Source code:-
try
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API);
request.ContentType = "multipart/form-data; boundary=" +boundary;
request.Method = "POST";
request.KeepAlive = true;
Stream memStream = new System.IO.MemoryStream();
var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +boundary + "\r\n");
var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +boundary + "--");
string headerTemplate =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
for (int i = 0; i < files.Length; i++)
{
memStream.Write(boundarybytes, 0, boundarybytes.Length);
var header = string.Format(headerTemplate, "uplTheFile", files[i]);
var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
using (var fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
{
var buffer = new byte[1024];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
}
}
memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
request.ContentLength = memStream.Length;
using (Stream requestStream = request.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
}
using (var response = request.GetResponse())
{
Stream stream2 = response.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
out_EndResult= new string [] {reader2.ReadToEnd()};
}
}
catch(Exception ex)
{
throw ex;
}
Inputs: Files as array, API that we need to give in the endpoint.