Thursday, February 14, 2019

Web Service > C# .Net > Getting an image from the file cabinet and Saving it locally

The objective of this article is to retrieve an image from the file cabinet and save it locally on your machine using the get operation in web services.

// specifies the internal id of the image that is in the file cabinet
RecordRef fileRef = new RecordRef();
fileRef.internalId = "2";
fileRef.typeSpecified = true;
fileRef.type = RecordType.file;

// retrieves the file from the file cabinet
var response = _service.get(fileRef);


if (response.status.isSuccess)
{
Console.WriteLine("successful response" );

// creates a record object
Record fileDownloaded = (Record)response.record;

// creates a new file
ns_ws_tester.com.netsuite.webservices.File NewFile = (ns_ws_tester.com.netsuite.webservices.File)fileDownloaded;
string fName = NewFile.name;
byte[] b1 = null;
b1 = NewFile.content;
Console.WriteLine("Name: " + fName);
Console.WriteLine("Media Type: " + NewFile.mediaTypeName);
Console.WriteLine("File Type: " + NewFile.fileType);

//saves the file in the specified folder. FileStream requires System.IO namespace
FileStream fs1 = null;
fs1 = new FileStream("C:\\dev\\" + fName + ".jpg", FileMode.Create);
fs1.Write(b1,0,b1.Length);
fs1.Close();
fs1 = null;
Console.Write("file downloaded locally");
}
else
{
Console.WriteLine("The file was not downloaded: ");
Console.WriteLine(getStatusDetails(response.status));
}

No comments:

Post a Comment