开发者

How to convert array of byte to original file (to provide download of file)

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.To开发者_高级运维Int32(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.


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);
    }
}


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

} 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜