C# 循环浏览temp目录中的多个文件,并使用现有行/id';将文件插入MS SQL数据库;s
我有一个包含现有行和附件ID的SQL数据库。我有一个包含数千个PDF文件的文件夹,需要插入到这个数据库中。文件应根据文件名/列插入每行。 例子。一个名为123.pdf的文件应插入ID为123的行中。 我已经使用Ajax文件上传工具创建了一个Asp.net web表单应用程序。如果我使用真实的目录,它就可以正常工作。如何使用临时目录执行此操作C# 循环浏览temp目录中的多个文件,并使用现有行/id';将文件插入MS SQL数据库;s,c#,asp.net,sql-server,asp.net-ajax,C#,Asp.net,Sql Server,Asp.net Ajax,我有一个包含现有行和附件ID的SQL数据库。我有一个包含数千个PDF文件的文件夹,需要插入到这个数据库中。文件应根据文件名/列插入每行。 例子。一个名为123.pdf的文件应插入ID为123的行中。 我已经使用Ajax文件上传工具创建了一个Asp.net web表单应用程序。如果我使用真实的目录,它就可以正常工作。如何使用临时目录执行此操作 protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolki
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
string filePath = e.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
switch (ext)
{
case ".pdf":
contenttype = "application/pdf";
break;
}
if (contenttype != String.Empty)
{
string tempPath = System.IO.Path.GetTempFileName();
AjaxFileUpload1.SaveAs(tempPath);
using (FileStream fs = File.OpenRead(tempPath))
{
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//How do I create a temp directory of the files in the AjaxFileUploader?
var dir = new DirectoryInfo(CreateTempDirectoryHere);
FileInfo[] pdfFiles = dir.GetFiles();
foreach (FileInfo pdfFile in pdfFiles)
{
var attachmentID = Path.GetFileNameWithoutExtension(pdfFile.ToString());
string constr = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("dbo.spUploadContentBulk", con);
cmd.Parameters.AddWithValue("@AttachmentID", attachmentID);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileName",
Value = filename
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileContent",
Value = bytes
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileType",
Value = contenttype
});
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
File.Delete(tempPath);
}
}
catch (Exception ex)
{
txtError.Text = ex.ToString();
}
}
我的意思是类似的东西(甚至不要使用临时文件夹):
我的意思是类似的东西(甚至不要使用临时文件夹):
我不太明白-您实际上在
e
中拥有关于文件的所有信息和文件的二进制内容。为什么不把它输入数据库;为什么您需要转到存储临时文件的磁盘?如果您想解析一个装满文件的文件夹,并将内容放入数据库,我不会使用FileUpload
工具,只需创建一个带有文件所在路径的常量,并枚举其中的内容。@Jonathan我希望用户能够拖放他们想要上载的文件,而不必创建一个只包含要上载的文件的文件夹。试图让它对用户来说尽可能简单。此外,如果文件夹中有几千个文件,我可能会限制每次上载20个。因此我认为我的第一点是:e
不包含文件的内容,或者至少让您通过e.GetContents()
或其他方式访问文件内容?你不能从上传的事件参数中读取数据,而不是从磁盘读取数据吗?@Jonathan那么我如何访问e.GetContents()
?类似这样的内容:FileInfo[]pdfFiles=e.GetContents()
;我不太明白-您实际上在e
中拥有关于文件的所有信息和文件的二进制内容。为什么不把它输入数据库;为什么您需要转到存储临时文件的磁盘?如果您想解析一个装满文件的文件夹,并将内容放入数据库,我不会使用FileUpload
工具,只需创建一个带有文件所在路径的常量,并枚举其中的内容。@Jonathan我希望用户能够拖放他们想要上载的文件,而不必创建一个只包含要上载的文件的文件夹。试图让它对用户来说尽可能简单。此外,如果文件夹中有几千个文件,我可能会限制每次上载20个。因此我认为我的第一点是:e
不包含文件的内容,或者至少让您通过e.GetContents()
或其他方式访问文件内容?你不能从上传的事件参数中读取数据,而不是从磁盘读取数据吗?@Jonathan那么我如何访问e.GetContents()
?类似这样的内容:FileInfo[]pdfFiles=e.GetContents()
;
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
try
{
string filename = e.FileName;
var bytes = e.GetContents();
var attachmentID = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
switch (ext)
{
case ".pdf":
contenttype = "application/pdf";
break;
}
if (contenttype != String.Empty)
{
string constr = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("dbo.spUploadContentBulk", con);
cmd.Parameters.AddWithValue("@AttachmentID", attachmentID);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileName",
Value = filename
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileContent",
Value = bytes
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@FileType",
Value = contenttype
});
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch (Exception ex)
{
txtError.Text = ex.ToString();
}
}