Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to upload a file using RESTSharp in vb.net.

I am not able to complete this.

Following is the code I am trying.

Public Function CreateUploadFileRequest(ByVal path As String, ByVal filename As String, ByVal fileStream As Stream) As RestRequest
            Dim request = New RestRequest(Method.POST)
            request.Timeout = Integer.MaxValue
            request.Resource = "{version}/files/dropbox{path}"
            request.AddParameter("version", _version, ParameterType.UrlSegment)
            request.AddParameter("path", path, ParameterType.UrlSegment)
            request.AddParameter("file", filename)
            request.AddFile("file", fileStream, filename) '---I am wrong at this line
            Return request
        End Function

I found C# code, but not able to convert a particular line in vb.net

request.AddFile(FieldName, (s) =>
        fileStream.CopyTo(s);
        fileStream.Flush();
    }, FileName, ContentType);

If I convert above into vb.net, then it does not work. Below is converted code.

request.AddFile("file", Function(s)
fileStream.CopyTo(s)
fileStream.Flush()
End Function, FileName, ContentType)

I also found one more line in c# but same is not working in vb.net after code convert.

[in C#] request.AddFile ("file", s => StreamUtils.CopyStream (fileStream, s), filename);

Converted to vb.net , [Not working]

[in vb.net] request.AddFile("file", Function(s) StreamUtils.CopyStream(fileStream, s), filename)
                You can simply pass a byte array there, along with the file name. So, File.ReadAllBytes(filePath) would do (also in-lined).
– Jimi
                Aug 6, 2020 at 17:34
                i do not have actual file, one of the other function is generating pdf file into stream, that function does not save that stream into file . So i want to use that pdf stream to upload using RESTSharp
– Teknas
                Aug 6, 2020 at 17:54
                If that is actually a generic Stream, try this: dim ms as new MemoryStream() fileStream.CopyTo(ms) then pass the array to the method: request.AddFile("file", ms.ToArray(), filename) '[...] ms.Dispose().
– Jimi
                Aug 6, 2020 at 18:13

Following is working:

dim ms as new MemoryStream()
request.AddFile("file", ms.ToArray(), "123.pdf_or_whateverfilename")
ms.Dispose()

Comment: ms will be filled by the respective function in my code. In my case, it will be written by the pdf generator function.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.