我需要从NetSuite中的FileCabinet下载文本文件。我能够搜索文件夹中的所有文件,并获取文件大小,名称和URL。但是,当我检查'content'属性时,它为NULL。如何在本地下载文件?
我尝试使用URL通过WebClient下载文件,但是它返回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);
//}
}
}
我希望“内容”包含文件内容。
执行搜索时,搜索结果不包括文件的内容,但是您确实具有文件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}");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句