单据上批量下载物料图片二开示例
比如在采购申请单中下载明细的物料对应的图片。物料图片分为数据库图片和文件服务器图片截图如下:
在采购申请单的表单插件中挂上插件,插件代码示例如下:
using ICSharpCode.SharpZipLib.Zip;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.FileServer.Core.Object;
using Kingdee.BOS.FileServer.ProxyService;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace Kingdee.BOS.Printing.PlugIn.Test
{
public class DownloadPic : AbstractBillPlugIn
{
public DownloadPic()
{ }
public override void BarItemClick(Core.DynamicForm.PlugIn.Args.BarItemClickEventArgs e)
{
base.BarItemClick(e);
if (e.BarItemKey == "ff_tbButton")
{
GetFMaterialIdPic();
}
}
private void GetFMaterialIdPic()
{
int count = this.View.Model.GetEntryRowCount("FEntity");
for (int i = 0; i < count; i++)
{
DynamicObject materialId = this.View.Model.GetValue("FMaterialId", i) as DynamicObject;
if (materialId != null)
GetPic(materialId["Id"]);
}
}
private void GetPic(object materialId)
{
string _temppath = HttpContext.Current.Server.MapPath(KeyConst.TEMPFILEPATH);
string tempZipDirPath = Path.Combine(_temppath, Guid.NewGuid().ToString());
Directory.CreateDirectory(tempZipDirPath);
string sql = string.Format("select FIMAGE,FIMAGEFILESERVER from T_BD_MATERIAL where FMaterialId={0}", materialId);
DynamicObjectCollection obj = DBServiceHelper.ExecuteDynamicObject(this.View.Context, sql, null);
if (obj != null)
{
var Image1 = obj[0]["FIMAGE"];
var ImageFileServer = obj[0]["FIMAGEFILESERVER"];
if (Image1 != null)
{
DownloadFileFromDatabase(tempZipDirPath,(Byte[])Image1);
}
if (ImageFileServer != null)
{
DownloadFileFromFileServer(tempZipDirPath, ImageFileServer.ToString());
}
}
CheckAndCompresseFile(tempZipDirPath);
}
private void DownloadFileFromDatabase(string tempZipFilePath, Byte[] image)
{
tempZipFilePath = Path.Combine(tempZipFilePath, Guid.NewGuid().ToString()+".webp");
using (FileStream fs = new FileStream(tempZipFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write(image);
}
}
private void DownloadFileFromFileServer(string tempZipFilePath, string fileId)
{
tempZipFilePath = Path.Combine(tempZipFilePath, Guid.NewGuid().ToString() + ".webp");
UpDownloadService upDownloadService = new UpDownloadService();
TFileInfo tFile = new TFileInfo()
{
FileId = fileId,
StartIndex = 0,
CTX = this.Context,
FilePath = tempZipFilePath
};
FileDownloadResult downloadResult = upDownloadService.DownloadSaveLocal(tFile);
}
private void CheckAndCompresseFile(string tempZipDirPath)
{
DirectoryInfo dirInfo = new DirectoryInfo(tempZipDirPath);
var _zipfilename = string.Format("Attachment_{0}.zip", Guid.NewGuid());
string _temppath = HttpContext.Current.Server.MapPath(KeyConst.TEMPFILEPATH);
string zipfilepath = Path.Combine(_temppath, _zipfilename);
CreateZipFile(tempZipDirPath, zipfilepath);
DeleteDirectory(tempZipDirPath);
string downloadUrl = PathUtils.ConvertPhysicalPathToDownloadUrl(this.Context, zipfilepath);
ViewCommonAction.ShowWebURL(this.View, downloadUrl);
}
private void CreateZipFile(string sourceDirectory, string zipFilePath)
{
if (!Directory.Exists(sourceDirectory))
{
return;
}
ZipOutputStream stream = null;
try
{
stream = new ZipOutputStream(File.Create(zipFilePath));
stream.SetLevel(0); // 压缩级别 0-9
byte[] buffer = new byte[4096]; //缓冲区大小
string[] filenames = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(file.Replace(sourceDirectory, "").TrimStart('\\'));
entry.DateTime = DateTime.Now;
stream.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
}
finally
{
if (stream != null)
{
stream.Finish();
stream.Close();
}
}
}
private void DeleteDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
DirectoryInfo[] childs = dir.GetDirectories();
foreach (DirectoryInfo child in childs)
{
child.Delete(true);
}
dir.Delete(true);
}
}
}
}
单据上批量下载物料图片二开示例
本文2024-09-23 04:19:44发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-164606.html