So I'm trying to upload a pdf file to a restapi. For some reason I the application cant get access to the files on my pc.
The code im using to upload:
public void Upload(string token, string FileName, string FileLocation, string Name, int TypeId, int AddressId, string CompanyName, string StreetNr, string Zip, string City, string CountryCode, string CustomFieldName, string CustomFieldValue)
{
var client = new HttpClient();
client.BaseAddress = _API.baseAddress;
//upload a new form
client.DefaultRequestHeaders.Date = DateTime.Now;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token);
using (var multiPartContent = new MultipartFormDataContent())
{
//get te bytes from a file
byte[] pdfData;
using (var pdf = new FileStream(@FileLocation, FileMode.Open))//Here i get the error.
{
pdfData = new byte[pdf.Length];
pdf.Read(pdfData, 0, (int)pdf.Length);
}
var fileContent = new ByteArrayContent(pdfData);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = FileName + ".pdf"
};
//add the bytes to the multipart message
multiPartContent.Add(fileContent);
//make a json message
var json = new FormRest
{
Name = Name,
TypeId = TypeId,
AddressId = AddressId,
CompanyName = CompanyName,
StreetNr = StreetNr,
Zip = Zip,
City = City,
CountryCode = CountryCode,
CustomFields = new List<CustomFieldRest>
{
new CustomFieldRest {Name = CustomFieldName, Value = CustomFieldValue}
}
};
var Content = new JsonContent(json);
//add the json message to the multipart message
multiPartContent.Add(Content);
var result = client.PostAsync("forms", multiPartContent).Result;
}
}
}