web.config 文件定义对应的参数,方便调用修改
--------------------------------------------------------------------》FTP 创建一个共性文件夹
在FTP服务器上共享一个文件夹,服务器上设置对应的 账号密码;
测 试的方法很简单,在浏览器【或者电脑网络路径】里输入\\{服务器}\{共享文件夹名},如果能输入用户和密码之后能看到你刚才随便建的那个文件,OK,共享是可以访问的
---------------------------------------------------------------------------------------->后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Ionic.Zip;
using System.IO;
using IFCA.Framework.Business.Tables.Linq;
using IFCA.Framework.Business.Tables.DBAdapter;
using IFCA.Files;
using System.Collections;
using IFCA.Framework.Business.Tables.EntityClasses;
using IFCA.Utility.Common;
using Newtonsoft.Json;
using System.Net;
using System.Data;
using System.Diagnostics;
namespace IFCA.BL.CM
{
///
/// 上传文件到指定服务器
///
public class FileTransferToServer
{
///
/// 本地附件路径
///
public static string UNC_PATH = System.Configuration.ConfigurationManager.AppSettings["UNC_Path"];
///
/// 服务器文件夹路径
///
public static string uriString = System.Configuration.ConfigurationManager.AppSettings["FTPShareUNC_Path"];
#region 上传文件至服务器
///
/// WebClient上传文件至服务器
///
public void PushDocumentToServer()
{
string fileNamePath = string.Empty;//文件名,全路径格式
string sql = string.Empty;
using (DataAccessAdapter adp = new DataAccessAdapter())
{
//获取尚未推送过附件的合同
sql = string.Format(@"SELECT doc.Documentid,doc.Documentname,doc.Documentpath,con.Contractno,type.Folder
FROM CF_Document doc
INNER JOIN CM_Contract con ON doc.Refrecordid=con.Contractid AND con.Recordstatus='Approved'
LEFT JOIN CF_Documenttype type ON doc.Documenttypeid =type.Documenttypeid
WHERE doc.Reftable='CM_Contract' AND doc.Recordstatus='Active'
AND NOT EXISTS(SELECT Documentid FROM CM_DocumentStatusPushEBS ds
WHERE Recordstatus='Active' AND Status='1' AND ds.Documentid=doc.Documentid) ");
DataTable dt = adp.GetDataTable(sql);
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
fileNamePath = Path.Combine(UNC_PATH, dt.Rows[i]["Documentpath"].ToStr());
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);//上传的附件名称
if(UpLoadFile(fileNamePath, uriString, dt.Rows[i]["Folder"].ToStr(), fileName))//上传成功后,插入相应的记录
{
var Folderpath= Path.Combine(dt.Rows[i]["Folder"].ToStr(), fileName);//FTP地址【子目录】
sql = string.Format(@"INSERT INTO dbo.CM_DocumentStatusPushEBS (Documentid,Contractno,Maincompresspackagename, Pushdate, Status, Recordstatus,Remark)
VALUES ({0}, '{1}','{2}', getdate(), '1', 'Active','');", dt.Rows[i]["Documentid"].ToStr(), dt.Rows[i]["Contractno"].ToStr(),Folderpath.ToStr());
adp.ExecuteNonQuery(sql);
//把附件信息(合同编号、FTP地址)推送到EBS
//dt.Rows[i]["Contractno"].ToStr(); Folderpath
//..........................................
}
else
{
sql = string.Format(@"INSERT INTO dbo.CM_DocumentStatusPushEBS (Documentid,Contractno,Maincompresspackagename, Pushdate, Status, Recordstatus,Remark)
VALUES ({0}, '{1}','{2}', getdate(), '0', 'Active','{3}');", dt.Rows[i]["Documentid"].ToStr(), dt.Rows[i]["Contractno"].ToStr(), "","需要上传的文件,在服务器上找不到,请检查!");
adp.ExecuteNonQuery(sql);
}
}
}
}
}
///
/// WebClient上传文件至服务器
///
/// 文件名,全路径格式
/// 服务器文件夹路径
/// 附件存放的子目录
/// 上传的附件名称
public bool UpLoadFile(string fileNamePath, string uriString, string Folder,string fileName)
{
if (!File.Exists(fileNamePath))//判断是否存在当前需要上传文件
{
return false;
}
// 创建WebClient实例
WebClient myWebClient = new WebClient();
string userName = System.Configuration.ConfigurationManager.AppSettings["FTPShare_userName"];
string passWord = System.Configuration.ConfigurationManager.AppSettings["FTPShare_passWord"];
string IP = System.Configuration.ConfigurationManager.AppSettings["FTPShare_IP"];
myWebClient.Credentials = new NetworkCredential(userName, passWord, IP);
uriString = Path.Combine(uriString, Folder);//合同的默认
if (!Directory.Exists(uriString))//如果不存在就创建file文件夹
{
Directory.CreateDirectory(uriString);
}
//要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
uriString = Path.Combine(uriString, fileName);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
try
{
//使用UploadFile方法可以用下面的格式
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
postStream.Close();
fs.Dispose();
}
else
{
postStream.Close();
fs.Dispose();
}
}
catch (Exception err)
{
postStream.Close();
fs.Dispose();
throw err;
}
finally
{
postStream.Close();
fs.Dispose();
}
return true;
}
#endregion
}
}
-----------------------------》方法调用
FileTransferToServer fileT = new FileTransferToServer();
fileT.PushDocumentToServer();
-----------------------》附件表
INSERT INTO dbo.CF_Document (Reftable, Refrecordid, Documentname, Documentpath, Keyword, Description, Documentdate, Filesize, Mimetype, Documenttypeid, Checkoutuserid, Recordstatus, Createuserid, Createdate, Modifyuserid, Modifydate, Directoryid, Documenttemppath, Syncoperationguid, Url, Rolename, AESKEY)
VALUES ('cm_budgetversion', '2', 'ProductTypeLib.xlsx', 'CM_BudgetVersion\9b274b49-0f0e-4b7d-85f2-d3e50bd2f411.xlsx', NULL, '', '2019-04-02 14:13:52.923', 10039, NULL, 642, NULL, 'Active', 1, '2019-04-02 14:13:52.923', 1, '2019-04-02 14:13:52.953', NULL, NULL, '1A66C79D-820C-4483-AA8B-92DC292127EC', '', NULL, '')
GO