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
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:
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