获取附件的文件流
金蝶云社区-assassinl10
assassinl10
1人赞赏了该文章 1,505次浏览 未经作者许可,禁止转载编辑于2018年06月05日 11:31:42

[code]

[password]123456[/password] Dictionary dictStream = GetAttachmentFileStream(this.Context, this.View.BusinessInfo.GetForm().Id,
this.View.Model.GetPKValue().ToString()); //附件

//操作完要释放文件流资源


#region 获取附件的文件流

public static Dictionary GetAttachmentFileStream(Context context, string formId, string pkValue)
{
Dictionary dictStream = new Dictionary(); //存储附件文件流
string tempDirPath = HttpContext.Current.Request.PhysicalApplicationPath + KeyConst.TEMPFILEPATH;
// 获取文件上传的临时目录

#region 获取附件

string filterStr = String.Format("FBILLTYPE='{0}' AND FINTERID='{1}'", formId, pkValue);
OQLFilter ofilter = OQLFilter.CreateHeadEntityFilter(filterStr);
DynamicObject[] attachArray = BusinessDataServiceHelper.Load(context, FormIdConst.BOS_Attachment, null,
ofilter);

#endregion

for (int i = 0; i < attachArray.Length; i++)
{
#region 获取文件内容

byte[] dataBuff = null;
if (Convert.ToInt16(attachArray[i]["FileStorage"]) != 0)
{
//不存储于数据库
if (FileServerHelper.UsedFileServer(context))
{
// 采用文件服务器存储
UpDownloadService service = new UpDownloadService();
TFileInfo tFile = new TFileInfo()
{
FileId = attachArray[i]["FileId"].ToString(),
CTX = context
};
dataBuff = service.GetFileData(tFile);
}
}
else
{
if (attachArray[i]["Attachment"] != null)
{
dataBuff = (byte[]) attachArray[i]["Attachment"];
}
}

#endregion

#region 文件内容转换为文件流

string fileName = Convert.ToString(attachArray[i]["AttachmentName"]); //文件名称
var tempFileName = string.Format("{0}_{1}_{2}", context.DBId, attachArray[i]["Id"], fileName);
// 拼接文件名
string fullName = System.IO.Path.Combine(tempDirPath, tempFileName); // 将附件保存至临时文件夹
System.IO.File.WriteAllBytes(fullName, dataBuff);
if (File.Exists(fullName))
{
FileStream stream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
dictStream.Add(fileName, stream); //文件名称,文件流
}

#endregion
}

return dictStream;
}

#endregion

[/code]