本文介绍了从不同位置保存文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码用于将文件保存到sql server



public byte [] ReadDoc(string document)

{

byte [] DocData = null;

DirectoryInfo dirInfo = new DirectoryInfo(document);

foreach(dirInfo.GetFiles()中的FileInfo f) />
{

long DocFileLength = f.Length;

FileStream fs1 = new FileStream(document,FileMode.Open,FileAccess.Read);

BinaryReader br1 = new BinaryReader(fs1);

DocData = br1.ReadBytes((int)DocFileLength);

}



返回DocData;



}

和保存按钮事件我有



if(listOfLoadedFiles.Items.Count!= 0)

{

byte [] fileData = null;



foreach(ListOfLoadedFiles.Items中的ListViewItem项目)

{



字符串扩展名= Path.GetExtension(item.Text);



fileData = ReadDoc(item.Text);



daData2.InsertCommand = new SqlCommand(INSERT INTO Documents VALUES(@ Document,@ patternID,@ userID,@ docmentName,@ extension),cs);



daData2.InsertCommand.Parameters.Add(@ Documents,SqlDbType.VarBinary).Value = fileData;

daData2.InsertCommand.Parameters.Add(@ patternID,SqlDbType.NVarChar).Value = patternID.Text;

daData2.InsertCommand.Parameters.Add(@ userID,SqlDbType.NVarChar).Value = ID。文字;

daData2.InsertCommand.Parameters.Add(@ documentName,SqlDbType.NVarChar).Value = item.Text;

daData2.InsertCommand.Parameters.Add( @extension,SqlDbType.NVarChar).Value = extension;



cs.Open();

daData2.InsertCommand.ExecuteNonQuery( );

cs.Close();



}



}

其他

{



}



,问题是当我从不同文件夹保存文件时,似乎FileInfo中保存的路径只记得最后一个



添加到listview的文件(listOfLo adedFiles),当我点击Save按钮时,我收到错误就行了



长DocFileLength = f.Length;无法找到该文件



任何帮助,谢谢

I have this code for saving files into sql server

public byte[] ReadDoc(string document)
{
byte[] DocData = null;
DirectoryInfo dirInfo = new DirectoryInfo(document);
foreach (FileInfo f in dirInfo.GetFiles())
{
long DocFileLength = f.Length;
FileStream fs1 = new FileStream(document, FileMode.Open, FileAccess.Read);
BinaryReader br1 = new BinaryReader(fs1);
DocData = br1.ReadBytes((int)DocFileLength);
}

return DocData;

}
and on save button event I have

if (listOfLoadedFiles.Items.Count != 0)
{
byte[] fileData = null;

foreach (ListViewItem item in listOfLoadedFiles.Items)
{

string extension = Path.GetExtension(item.Text);

fileData = ReadDoc(item.Text);

daData2.InsertCommand = new SqlCommand("INSERT INTO Documents VALUES (@Documents, @patternID, @userID, @documentName, @extension)", cs);

daData2.InsertCommand.Parameters.Add("@Documents", SqlDbType.VarBinary).Value = fileData;
daData2.InsertCommand.Parameters.Add("@patternID", SqlDbType.NVarChar).Value = patternID.Text;
daData2.InsertCommand.Parameters.Add("@userID", SqlDbType.NVarChar).Value = ID.Text;
daData2.InsertCommand.Parameters.Add("@documentName", SqlDbType.NVarChar).Value = item.Text;
daData2.InsertCommand.Parameters.Add("@extension", SqlDbType.NVarChar).Value = extension;

cs.Open();
daData2.InsertCommand.ExecuteNonQuery();
cs.Close();

}

}
else
{

}

and the problem is when I am saving files from different folders, it seems that saved path in FileInfo remebers only the last

added file to listview (listOfLoadedFiles), and when I click Save button, i get error on line

long DocFileLength = f.Length; that file couldn't be found

any help, thank you

推荐答案

foreach (ListViewItem item in listOfLoadedFiles.Items)
    {
    string path = item.Text;
    if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
        {
        byte[] data = File.ReadAllBytes(path);
        long length = new FileInfo(path).Length;
        string extension = Path.GetExtension(path);
        ... continue and write the file to SQL.
        }
    }



完全转储ReadDoc方法!


And dump the ReadDoc method completely!



这篇关于从不同位置保存文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 07:04