【发布时间】:2018-01-04 21:10:31
【问题描述】:
我想从给定的路径上传一个文件,该文件应该是一个 IP 地址,并在那里搜索一个 Excel 文件并上传到数据库。
Excel 文件将有多个要上传的工作表。 我还想将不同的工作表存储到数据库中的不同表中。
如何在 ASP.NET 中做到这一点。
【问题讨论】:
标签: c# sql asp.net sql-server
我想从给定的路径上传一个文件,该文件应该是一个 IP 地址,并在那里搜索一个 Excel 文件并上传到数据库。
Excel 文件将有多个要上传的工作表。 我还想将不同的工作表存储到数据库中的不同表中。
如何在 ASP.NET 中做到这一点。
【问题讨论】:
标签: c# sql asp.net sql-server
如果您使用的是 FTP 服务器,则表示如下所示。以下示例在没有数据库的情况下工作正常。
string CompletePath = "C:/Doc/test.xlsx"; //path for file
private FtpWebRequest FTPDetail(string FileName)
{
string uri = "";
string serverIp = "255.255.255.1"; //Ftp server IP address
string Username = "test"; // Ftp User name
string Password = "test123"; // Ftp Password
uri = "ftp://" + serverIp + "/root/" + FileName;
FtpWebRequest objFTP;
objFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
objFTP.Credentials = new NetworkCredential(Username, Password);
objFTP.UsePassive = true;
objFTP.KeepAlive = false;
objFTP.Proxy = null;
objFTP.UseBinary = false;
objFTP.Timeout = 90000;
return objFTP;
}
public bool UploadFile()
{
FtpWebRequest objFTP= null;
try
{
objFTP= FTPDetail("File.xlsx");
objFTP.Method = WebRequestMethods.Ftp.UploadFile;
using (FileStream fs = File.OpenRead(CompletePath))
{
byte[] buff = new byte[fs.Length];
using (Stream strm = objFTP.GetRequestStream())
{
contentLen = fs.Read(buff, 0, buff.Length);
while (contentLen != 0)
{
strm.Write(buff, 0, buff.Length);
contentLen = fs.Read(buff, 0, buff.Length);
}
objFTP = null;
}
}
return true;
}
catch (Exception Ex)
{
if (objFTP!= null)
{
objFTP.Abort();
}
throw Ex;
}
}
【讨论】: