cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP Utility Multipart

SireeshaJaggems
Level 3
Hello Everyone,

I am using HTTP Utility Multipart VBO, to upload file to Web API(form-data). I am able to upload file, but file turns out to be blank after uploading . Iam passing Headers, URL and FileMetaData(filepath).Kindly , let me understand why the content is missing in the uploaded file.
4 REPLIES 4

Hi Sireesha,

What is the response status that you get? Is it 200/201? Also have you already tried the same HTTP request from POSTMAN? Would you be able to share details on the request parameters and the API being used or any API documentation for the API that you are calling.

PragatiPatnaik
Level 2
@SireeshaJaggems : Even I have the same query(File is getting corrupted post uploading into application) too. Can you please share your solution if it is already resolved.​​
@shashank.kumar280 : Yes it is working fine in Postman for my solution. The status is 200 in Postman
.
Postman Details :

Accept: */*
Cache-Control: no-cache
Postman-Token: 4b24317d-edef-4ca5-bf97-c3767434b1,70ace7f6-f3dc-4876-ade9-c46de5b54fed
cookie: JSESSIONID=0000IypvyfzKGyD9J5oRV5R:1chgff5g
accept-encoding: gzip, deflate
content-type: multipart/form-data; boundary=--------------------------249786833778856564545
content-length: 260868
Connection: keep-alive
cache-control: no-cache
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZo7WV


Content-Disposition: form-data; name="uploadFile"; filename="/C:/Users/45078310/Desktop/1076595308.pdf


------WebKitFormBoundary7MA4YWxkTrZo7WV--
​​

SireeshaJaggems
Level 3
@Pragati Patnaik

I couldn't  find the resolution for the issue with the http VBO, so I developed below C# code to upload the files. 

status = null;
string url = URL;  // web api url
string file = FilePath; // path of file to upload
string paramName = "file";
// string contentType = "text/plain";

string username = null;
string password = null;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

foreach (DataRow r in headers.Rows)
{
foreach (DataColumn c in headers.Columns)
{
string columnName = c.ColumnName;
string val = (r[columnName]).ToString();
wr.Headers.Add(columnName, val);
}
}

wr.Headers.Add("Authorization", "Basic");
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;

Stream rs = wr.GetRequestStream();

string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";

rs.Write(boundarybytes, 0, boundarybytes.Length);


string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

// var mimeType = MimeMapping.GetMimeMapping(file);
// contentType = MimeMapping.MimeUtility.GetMimeMapping(file);
// string mimeType = System.Web.MimeMapping.GetMimeMapping(file);

string filename = Path.GetFileName(file);
//contentType=MimeMapping.GetMimeMapping(filename);
string header = string.Format(headerTemplate, paramName, filename, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);

FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();

byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();

WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
status = "success";
//log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
status = reader.ReadToEnd();
}

if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}

Hope this helps​.​

@Pragati Patnaik Could you confirm what parameters were passed in FileMetadata collection and FieldMetadata collection?​