Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have a small project I am working on. The project comprises a web api, and a powershell script.

The objective scenario is:

In the WebAPI I upload a text file, then convert the file to a memorystream, the memorystream is a property of a result object with is returned from the API call. The API call is returning clean Json.

In powershell
1. I am calling the API.
2. Creating a folder
3. Creating a file (filename provided from the api)
4. Writing the fileContent to the file (* this is ordinary text, passed as a memorystream from c#)


I could simply have passed string instead of memorystream, but it appears if my textfile is very large the eventual content placed into the file in powershell is truncated. Hence my reason for choosing memorystream.

I could be completely doing it wrong by passing memorystream in my json results to any consumer of the api. So do feel free to guide me here.

Code Behind API

Conversion of file to memorystream

C#
public MemoryStream getMemoryStream(string fileName)
    {
      using (FileStream fileStream = File.OpenRead(fileName))
      {
        MemoryStream memStream = new MemoryStream();
        memStream.SetLength(fileStream.Length);
        fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
        return memStream;
      }
    }


The Object:

C#
public class UploadedFile
  {
    public string filename { get; set; }
    public MemoryStream fileContent { get; set; }
    public bool Result { get; set; }
  }


From Powershell I have the following code:

cls
Add-Type -AssemblyName System.ServiceModel.Web, System.Runtime.Serialization

$ResultsUrl = "http://localhost/MovieNight.WebAPI/api/ByteTransfer/uploadedFile"

$ReceivedPacket = Invoke-WebRequest -Uri $ResultsUrl -Method Get  | ConvertFrom-Json

if($ReceivedPacket -ne $null)
{
    $SaveToLocation = "C:\Data\Scripts\UploadedFiles\test"

    [System.IO.Directory]::CreateDirectory($SaveToLocation)

    $SaveToLocation = $SaveToLocation + "\" + $ReceivedPacket.filename

    $ReceivedPacket.fileContent | Out-File -FilePath $SaveToLocation  -Encoding utf8


    $ReceivedPacket.fileContent | Out-File -FilePath $SaveToLocation  -Encoding utf8

    $ReceivedPacket | gm # placed here to check what is created

}



Opening the created file shows me the content as follows:

buffer     : //5VAFMARQAgAFsASwBvAG0AbQB1AG4AaQBrADgAQwBsAG8AdQBkAE8AbgBlAF0ADQAKAEcATwANAAoALwAqACoAKgAqACoAKgAgAE8AYgBqAGUAYwB0ADoAIAAgAEYAbwByAGUAaQBnAG4ASwBlAHkAIABbAEYASwBfAF8
              AQwB1AHMAdABvAG0AZQByAEEAXwBfAEEAYwBjAG8AdQBfAF8ANABCAEEAQwAzAEYAMgA5AF0AIAAgACAAIABTAGMAcgBpAHAAdAAgAEQAYQB0AGUAOgAgADEAMQAvADEANgAvADIAMAAxADQAIAAxADgAOgAxADMAOgAxAD
... blob removed for space ...
_origin     : 0
_position   : 0
_length     : 44848
_capacity   : 44848
_expandable : True
_writable   : True
_exposable  : True
_isOpen     : True
__identity  : 


My question is. Is it possible to convert the memorystream to the original text read from the API helper methods in powershell? If yes, any links or code samples would be greatly appreciated.

Thank you
Posted

1 solution

Well I did some more playing around and made some changes to the application, while maintaining my desired goal.

Let me begin by mentioning what I had done wrong:

1. Using memorystream (am not yet skilled enough to use work with this)
2. In powershell was fighting with memorystream and byte array (totally lost here)

What did I do to fix the problem?

API Side

I changed my response object property that had been memorystream to byte[]

C#
public class UploadedFile
  {
    public string filename { get; set; }
    public byte[] fileContent { get; set; }
    public bool Result { get; set; }
  }


Powershell Script

$decoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($fileContent))

      $decoded | Out-File -FilePath $SaveToLocation -Encoding ascii


Now I am able to output the file contents that I have passed via the wire and well problem solved, by the way I am only handling textfiles.

Should you have comments on how the above solution can be optimized to be better, do fee free to comment.

Happy coding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900