如何使用 C# 从 NetSuite 中的 FileCabinet 下载文件
How to download file from FileCabinet in NetSuite using C#
我需要从 NetSuite 的 FileCabinet 下载文本文件。我能够搜索文件夹中的所有文件并取回文件大小、名称和 URL。但是当我检查 'content' 属性 时,它是 NULL。如何在本地下载文件?
我尝试使用 URL 使用 WebClient 下载文件,但它 returns 403 是有道理的。
var result = Client.Service.search(fileSearch);
var recordList = (Record[])result.recordList;
if (recordList != null && recordList.Length != 0)
{
foreach (var item in recordList)
{
var file = (com.netsuite.webservices.File)item;
int fileSize = (int)file.fileSize; // Returns the correct file size
byte[] fileContent = file.content; // NULL reference ??
Console.WriteLine(file.url + " ==== " + file.name );
// How to download the File from the url above??
// Can't do this, 403 error, below client dont use the same security context
//using (var client = new WebClient())
//{
// client.UseDefaultCredentials = false;
// client.DownloadFile(baseUrl + file.url, file.name);
//}
}
}
我希望 'content' 包含文件内容。
当您执行搜索时,搜索结果不包括文件的内容,但您确实有文件 ID。下面是 NetSuite 服务的扩展方法,通过它的 id 获取文件:
public static NetSuite.File GetFileById(this NetSuiteService ns, int fileId)
{
var file = new NetSuite.File();
var response = ns.get(new RecordRef()
{
type = RecordType.file,
internalId = fileId.ToString(),
typeSpecified = true
});
if (response.status.isSuccess)
{
file = response.record as File;
}
return file;
}
var f = ns.GetFileById(3946);
var path = Path.Combine(Directory.GetCurrentDirectory(), f.name);
var contents = f.content;
System.IO.File.WriteAllBytes(path, contents);
Console.WriteLine($"Downloaded {f.name}");
我需要从 NetSuite 的 FileCabinet 下载文本文件。我能够搜索文件夹中的所有文件并取回文件大小、名称和 URL。但是当我检查 'content' 属性 时,它是 NULL。如何在本地下载文件?
我尝试使用 URL 使用 WebClient 下载文件,但它 returns 403 是有道理的。
var result = Client.Service.search(fileSearch);
var recordList = (Record[])result.recordList;
if (recordList != null && recordList.Length != 0)
{
foreach (var item in recordList)
{
var file = (com.netsuite.webservices.File)item;
int fileSize = (int)file.fileSize; // Returns the correct file size
byte[] fileContent = file.content; // NULL reference ??
Console.WriteLine(file.url + " ==== " + file.name );
// How to download the File from the url above??
// Can't do this, 403 error, below client dont use the same security context
//using (var client = new WebClient())
//{
// client.UseDefaultCredentials = false;
// client.DownloadFile(baseUrl + file.url, file.name);
//}
}
}
我希望 'content' 包含文件内容。
当您执行搜索时,搜索结果不包括文件的内容,但您确实有文件 ID。下面是 NetSuite 服务的扩展方法,通过它的 id 获取文件:
public static NetSuite.File GetFileById(this NetSuiteService ns, int fileId)
{
var file = new NetSuite.File();
var response = ns.get(new RecordRef()
{
type = RecordType.file,
internalId = fileId.ToString(),
typeSpecified = true
});
if (response.status.isSuccess)
{
file = response.record as File;
}
return file;
}
var f = ns.GetFileById(3946);
var path = Path.Combine(Directory.GetCurrentDirectory(), f.name);
var contents = f.content;
System.IO.File.WriteAllBytes(path, contents);
Console.WriteLine($"Downloaded {f.name}");