I am trying too upload a file with C# by an API.
The StreamUtils dont work, I am getting error: "Cannot convert lambda expression to type 'string' because it is not a delegate type".
Any idea on how to upload the file? It is around 100MB.
public void UploadModel(string ProjectId, string filename, Stream fileStream)
{
string access_token_string = Read_Json_Values("access_token");
string webstring = String.Format("https://api.test.com/v2/projects/{0}/revisions", ProjectId);
var client = new RestClient(webstring);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + access_token_string);
request.AddHeader("Content-Type", "application/ifc");
request.AddHeader("xxx-Params", "{\"callbackUri\": \"https://example.com\", \"filename\": \"mk337.ifc\", \"comment\": \"From Postman\", \"model\": \"21312312312312\"}");
request.AddFile("file", s => StreamUtils.CopyStream(fileStream, s), filename);
IRestResponse response = client.Execute(request);
Console.WriteLine("Model is uploaded!");
}
internal static class StreamUtils
{
private const int STREAM_BUFFER_SIZE = 128 * 1024; // 128KB
public static void CopyStream(Stream source, Stream target)
{ CopyStream(source, target, new byte[STREAM_BUFFER_SIZE]); }
public static void CopyStream(Stream source, Stream target, byte[] buffer)
{
if (source == null) throw new ArgumentNullException("source");
if (target == null) throw new ArgumentNullException("target");
if (buffer == null) buffer = new byte[STREAM_BUFFER_SIZE];
int bufferLength = buffer.Length;
int bytesRead = 0;
while ((bytesRead = source.Read(buffer, 0, bufferLength)) > 0)
target.Write(buffer, 0, bytesRead);
}