如何将字节数组转换为原始文件(提供文件下载)

发布于 2024-11-19 05:03:17 字数 930 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…楫夜泊 2024-11-26 05:03:17

一种方法 - 不一定是最好的 - 如下:

using (MemoryStream ms = new MemoryStream(theBytes)) 
{
    using (FileStream fs = new FileStream(string.Format("C:\\tempfile.{0}", theExtension))) 
    {
        ms.WriteTo(fs);
    }
}

One way - not necessarily the best - is as follows:

using (MemoryStream ms = new MemoryStream(theBytes)) 
{
    using (FileStream fs = new FileStream(string.Format("C:\\tempfile.{0}", theExtension))) 
    {
        ms.WriteTo(fs);
    }
}
绾颜 2024-11-26 05:03:17
namespace FileSaveDialogDemo  
{  

public partial class MainPage : UserControl  
{ 
    #region Fields  
    SaveFileDialog dialog= new SaveFileDialog(); 
    #endregion  

    #region Constructors  
    public MainPage()  
    {  
        InitializeComponent();  
        this.dialog = new SaveFileDialog();  
        try 
        {  
            this.dialog.DefaultExt = ".txt";  
            this.dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";  
            this.dialog.FilterIndex = 2;  
        }  
        catch ( Exception ex )  
        {  
            this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message;  
        }  
    } 
    #endregion  

    private void btnSaveFile_Click( object sender, RoutedEventArgs e )  
    {  
        bool? dialogResult = this.dialog.ShowDialog();  

        if ( dialogResult == true )  
        {  
            try 
            {  
               byte[] fileBytes; // your varbinary file from database
               using (Stream fs = (Stream)dialog.OpenFile())
                {
                fs.Write(fileBytes, 0, fileBytes.Length);
                fs.Close();
                lblMsg.Content = "File successfully saved!";
               }   

            }  
            catch ( Exception ex )  
            {  
                this.tblError.Text = "Error calling service: " + ex.Message; 
            }  
        }  
    }  // End of Function
 }// End of MainPage class

} 
namespace FileSaveDialogDemo  
{  

public partial class MainPage : UserControl  
{ 
    #region Fields  
    SaveFileDialog dialog= new SaveFileDialog(); 
    #endregion  

    #region Constructors  
    public MainPage()  
    {  
        InitializeComponent();  
        this.dialog = new SaveFileDialog();  
        try 
        {  
            this.dialog.DefaultExt = ".txt";  
            this.dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";  
            this.dialog.FilterIndex = 2;  
        }  
        catch ( Exception ex )  
        {  
            this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message;  
        }  
    } 
    #endregion  

    private void btnSaveFile_Click( object sender, RoutedEventArgs e )  
    {  
        bool? dialogResult = this.dialog.ShowDialog();  

        if ( dialogResult == true )  
        {  
            try 
            {  
               byte[] fileBytes; // your varbinary file from database
               using (Stream fs = (Stream)dialog.OpenFile())
                {
                fs.Write(fileBytes, 0, fileBytes.Length);
                fs.Close();
                lblMsg.Content = "File successfully saved!";
               }   

            }  
            catch ( Exception ex )  
            {  
                this.tblError.Text = "Error calling service: " + ex.Message; 
            }  
        }  
    }  // End of Function
 }// End of MainPage class

} 
牛↙奶布丁 2024-11-26 05:03:17

看来您遇到的问题可能与保存二进制文件无关;这更有可能是一个基本的安全问题。尝试保存到您具有编程写入权限的路径。例如,尝试保存到“我的文档”目录而不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文