如何将字节数组转换为原始文件(提供文件下载)
我正在使用 Opendialogbox 来读取该文件。然后将文件存储在 byte[] 数组中。
文件-->字节 [] 字节[] -->存储在 SQL AZure 上的 varbinary(max) 字段中。
这是我的代码:
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
FileStream fileStream = ofd.File.OpenRead());
byte[] buffer = new byte[fileStream.Length];
int read = 0;
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
do
{
read = binaryReader.Read(buffer, 0, Convert.ToInt32(fileStream.Length));
// Stored the File in byte[] Array buffer
} while (read > 0);
}
}
现在我想将此字节数组转换为原始文件(如 .doc、.txt、jpeg)。我知道要转换的文件的扩展名。
SQL AZure --->字节[] // 完成 字节[] --->到原始文件。 // 问题
请给出下载文件的解决方案。
I m using Opendialogbox to read the file. Then stored the file in byte[] array.
file --> byte []
byte[] --> stored on SQL AZure in varbinary(max) field.
Here is my code:
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
FileStream fileStream = ofd.File.OpenRead());
byte[] buffer = new byte[fileStream.Length];
int read = 0;
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
do
{
read = binaryReader.Read(buffer, 0, Convert.ToInt32(fileStream.Length));
// Stored the File in byte[] Array buffer
} while (read > 0);
}
}
Now I want to convert this byte array to the original file (like .doc,.txt,jpeg). i know the extension in which file is to be convert.
SQL AZure ---> byte[] // done
byte[] ---> to original file. // Problem
Please give solution to download the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法 - 不一定是最好的 - 如下:
One way - not necessarily the best - is as follows:
看来您遇到的问题可能与保存二进制文件无关;这更有可能是一个基本的安全问题。尝试保存到您具有编程写入权限的路径。例如,尝试保存到“我的文档”目录而不是 C:。尝试像这样使用Environment.SpecialFolder 枚举,并附加文件名+扩展名。
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
您还有许多其他方法可以解决此问题,包括以提升模式启动 Visual Studio(以管理员身份运行),和/或允许“每个人”对 C:\驾驶。但我不一定推荐这些技术;考虑保存到安全设置低于 c:\ 的文件夹,例如“我的文档”。
It seems the issue you have has probably nothing to do with saving a binary file; it is more likely a basic security issue. Try saving to a path on which you have programmatic write access. For example, try saving to your My Documents directory instead of C:. Try using the Environment.SpecialFolder enumeration like this, and append the file name + extension.
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
You have a number of other ways to work around this issue, including starting Visual Studio in Elevated Mode (run as Administrator), and/or allow "Everyone" write access to your C:\ drive. But I wouldn't recommend these techniques necessarily; consider saving to a folder where the security settings are lower than c:\, such as My Documents.