Retrieving Binary/Blob files from Microsoft Dynamics Nav wit

John777
Member Posts: 4
I am working with a MS Dynamics Nav Database that have a file attachment tables. The files are stored in MS SQL. I am able to pull the files to my desktop with a custom asp.net application that I have built, but when I open the files, they are corrupted. These are PDFs files that are located in the "image" file type column of the database and I have tried to download over 20 files. All of them varies in size and seem to download successfully.
The reason why I suspect these are PDFs files is because the column right next to the binary columns give me the name of the file as in PDF format. I have also tried to renaming the file after I download to different image formats but without any luck when I tried to open it. This is not my first project to retrieve binary files, from MS SQL database. If anyone work on getting files off the Nav database before, please help me. The sample code below I wrote to retrieve files using LINQ to SQL when I give it a specific ID in the browser. Please advice me if you know any sort of compression or encryption in the binary files itself and how to grab the file successfully to read it. Thanks
The reason why I suspect these are PDFs files is because the column right next to the binary columns give me the name of the file as in PDF format. I have also tried to renaming the file after I download to different image formats but without any luck when I tried to open it. This is not my first project to retrieve binary files, from MS SQL database. If anyone work on getting files off the Nav database before, please help me. The sample code below I wrote to retrieve files using LINQ to SQL when I give it a specific ID in the browser. Please advice me if you know any sort of compression or encryption in the binary files itself and how to grab the file successfully to read it. Thanks
protected void getFileFromID(string queryid) { string Filename = string.Empty; byte[] bytes; try { DataClassesFilesDataContext dcontext = new DataClassesFilesDataContext(); var myfile = (from file in dcontext.Comptroller_File_Attachments where file.No_ == queryid select file).First(); if (myfile.Table_ID.ToString().Length > 0 && myfile.Attachment != null) { Filename = myfile.FileName.ToString(); bytes = myfile.Attachment.ToArray(); Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename); Response.BinaryWrite(bytes); Response.End(); } else { Response.Write("no file exist"); } } catch (Exception e) { Response.Write(e); } }
0
Comments
-
I have done this, here is a part of the webpage I created.query = @SELECT [Link ID], , [File Name], [Content Type], [Object] FROM [ + databaseName +
@].dbo.[ + linkTableName + @] WHERE [Link ID] =' + linkId + @';
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = query;
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
sqlDataReader.Read();
fileName = sqlDataReader.GetValue(2).ToString();
contentType = sqlDataReader.GetValue(3).ToString();
_ImageBytes = (Byte[])(sqlDataReader.GetValue(4));
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "startUpScriptBlock", "alert('No document found with " + linkId + " calling: " + query +"');", true);
sqlConnection.Close();
return;
}
{
Page.Response.Clear();
Page.Response.Expires = 0;
Page.Response.Buffer = false;
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Page.Response.AddHeader("Content-Length", _ImageBytes.Length.ToString());
Page.Response.ContentType = ContentType;
Page.Response.BinaryWrite(_ImageBytes);
Page.Response.End();
}________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com0 -
so no option to do this thru NAV?0
-
Sure there is.
You can easilly insert and select entries from SQL database with a field type Image. In NAV you handle the field as a byte array.
You can also download the file from the web page into NAV with HttpWebRequest.
Everything is possible with DotNet________________________________
Gunnar Gestsson
Microsoft Certified IT Professional
Dynamics NAV MVP
http://www.dynamics.is
http://Objects4NAV.com0 -
Sorry. Thanks for the answer. But I figured out the problem. The way that I found to decompress these files are to remove the first 4 bytes in the array of bytes of the BLOB file and then use deflatestream to retrieve the file.0
-
ok, good, so the blob is created in the NAV client? and now you get the data in c#
and for this you need to remove 4 bytes. Is it possible to show a codesnippet?0 -
Code snippet is below and required .net >= 4.0. You can re-write it to your liking or optimize it. I am using LINQ-SQL but you can change the filename to a public variable in a separate class file to avoid using tuple and re-run of the function. Basically, it is taking in a fileID with a query parameter at the end of the URL. Here it is:
//get bytes and remove first 4 bytes from bytes array protected Tuple<byte[], string> getBytesfromFile(string queryID) { byte[] MyFilebytes = null; string filename = string.Empty; try { DataClassesFilesDataContext dcontext = new DataClassesFilesDataContext(); var myfile = (from file in dcontext.Comptroller_File_Attachments where file.No_ == queryID select file).First(); if (myfile.Table_ID.ToString().Length > 0 && myfile.Attachment != null) { MyFilebytes = myfile.Attachment.ToArray().Skip(4).ToArray(); filename = myfile.FileName.ToString(); } else Response.Write("no byte to return"); } catch { Response.Write("no byte"); } return Tuple.Create(MyFilebytes, filename); } //after getting the remaining bytes (after removing 4 first byte) deflate the byte and then store it in a memory steam and get the result back. protected void getFile() { try { string Filename = string.Empty; byte[] myfile = getBytesfromFile(getQueryID()).Item1; byte[] result; using (Stream input = new DeflateStream(new MemoryStream(myfile), CompressionMode.Decompress)) { using (MemoryStream output = new MemoryStream()) { input.CopyTo(output); result = output.ToArray(); } } Filename = getBytesfromFile(getQueryID()).Item2; Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename); Response.BinaryWrite(result); Response.End(); } catch (Exception e) { Response.Write(e); } } //pass in file id protected string getQueryID() { QueryID.QueryStringID = Request.QueryString["fileid"]; return QueryID.QueryStringID; }
0 -
Thanks for sharing, really good info.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions