如何正确保存和打开文件(不破坏文件) [英] How to save and open files correctly ( without destroying file )
问题描述
这是我的代码:
this is my code :
private void BtnDisplay_Click(object sender, EventArgs e)
{
if (GridFile.CurrentCell == null)
{
MessageBox.Show("select a row");
return;
}
DataTable dt = new DataTable();
int SelectedRow = GridFile.CurrentCell.RowIndex;
string OriginalPath = GridFile.SelectedRows[0].Cells[2].Value.ToString();
string tempsafefilename = GridFile.SelectedRows[0].Cells[5].Value.ToString();
saveFileDialog1.FileName = OriginalPath;
string FileName = saveFileDialog1.FileName;
//.......
if (File.Exists(FileName))
{
Process.Start(FileName);
}
else
{
byte[] FileData = new byte[32768];
FileData = (byte[])DS.Tables["TeacherFile"].Rows[SelectedRow]["FileData"];
String newfile = @"D:\Temp";
DirectoryInfo l_dDirInfo = new DirectoryInfo(newfile);
if (l_dDirInfo.Exists == false)
{
Directory.CreateDirectory(newfile);
}
string NewPath = newfile + "\\" + tempsafefilename;
//FileStream fStream = new FileStream(NewPath, FileMode.Create);
//while (fStream.Read(FileData, 0, FileData.Length) > 0)
//{
// Process.Start(NewPath);
//}
using (FileStream fStream = new FileStream(NewPath, FileMode.Create))
{
fStream.Write(FileData, 0, FileData.Length);
FileData = ReadToEnd(fStream);
fStream.Write(FileData, 0, FileData.Length);
Process.Start(NewPath);
}
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = 0;
if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
}
}
//....OKKKKKK
byte[] ReadFile(string sPath)
{
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[32768];
int read = 0;
int chunk;
fStream.Position = 3147483648;
while ((chunk = fStream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = fStream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
我的程序所做的是将文件(任何文件 - 电影 - 音乐 - zip ...)转换为代码(0和1) - 我想)并将其保存到我的SQL数据库中。现在,当我点击(打开文件 - 显示)按钮时,它会显示文件,但文件已被销毁。下面的链接是veronica movie的图片(大约700 MB)我用我的软件保存它,当我点击打开时,我的软件打开了文件,但它已损坏,KMPlayer向我显示这些代码。怎么解决?请帮帮我。
https://i.imgsafe.org/ f52cc7409c.png [ ^ ]
我尝试了什么:
i我的旧代码在这里:
http://snipsave.com/user/profile/johnnyflow#14098
what my program does, is that convert a file (any file- movie - music - zip...) to the codes (0 and 1 - I think) and save it to my SQL database. now when I click on (open file - show) button it show me the file but the file is destroyed. the below link is a picture of "veronica movie" ( it's about 700 MB) I save it with my software and when I clicked on open, my software opened the file but it was corrupted and the KMPlayer show me those codes. how to fix it? please help me.
https://i.imgsafe.org/f52cc7409c.png[^]
What I have tried:
i put my old codes here :
http://snipsave.com/user/profile/johnnyflow#14098
推荐答案
你很可能不是'保存数据库中的正确数据,或者您没有检索它并再次正确保存它。
在SQL中保存文件的最简单方法是将其直接加载到字节数组中,保存:
The chances are that you aren't saving the right data in the DB, or you aren't retrieving it and saving it again properly.
The easiest way to save a file in SQL is to load it directly into a byte array, and save that:
byte[] data = File.readAllBytes(pathToFile);
将字节写回文件也很简单:
And to write the bytes back to a file is also trivial:
File.WriteAllBytes(pathToFile, data);
这里有关于如何保存和检索数据的信息:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它基于图像,但视频数据的原理完全相同。
但是......我不会将视频数据存储在数据库中:它是大数据,它会导致系统出现很多瓶颈以保存和检索它。
相反,将视频存储为文件并将文件位置存储在数据库中。
我这样做的方法是使用File将文件复制到带有Guid文件名的已知位置。复制然后将基于Guid的路径加上原始文件名存储在数据库中。
然后,您可以使用File.Copy重新创建原始文件,以便在需要时将其重新放回。
There is info on how to save and retrieve it to the DB here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it's based aroung images, but the principles are exactly the same for video data.
But...I wouldn't store video data in a database: it's big data and it will cause a lot of bottlenecks on your system to save and retrieve it.
Instead, store the video as a file and store the file location in the DB.
The way I'd do it is to copy the file to a "known" location with a Guid filename using File.Copy and then store the Guid-based path plus the original file name in the DB.
You can then recreate the original by using File.Copy to put it back when needed.
这篇关于如何正确保存和打开文件(不破坏文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!