Showing posts with label functionality. Show all posts
Showing posts with label functionality. Show all posts

Thursday, March 29, 2012

Office 2007 documents to database, dynamically generate on demand

Hello, I am currently adding some document management functionality to a .Net 2.0 Web app (VB) where I take a file in, save the file to a SQL 2000 database (image datatype), and then allow users to browse and download these files.

This works great for most files my users use -- with the exception of Office 2k7 files

I am able to upload a file, read the file into a binary array and insert into a new record in the database.

However, when I retrieve the binary array and attempt to output the file, there is a problem.

The user is prompted to open or save the file per-usual, however when Word 2007 tries to open the file, there is an error message that says:

"The Office Open XML file Test.docx cannot be opened because there are problems with the contents"

Under "Details" it says:

"The file is corrupt and cannot be opened"

If I click "OK", I am asked whether I want to have Word recover the contents of the document.

If I click "Yes" my document opens and all of the original content is there.

I have been doing some reading, and I know that the new Office 2007 files are really .zip files with XML files that contain formatting and content information.

What I would like to know is, is there a reason why I can't save these files to the database as I have been, and if so, is there a "right" way to do this?

Hi,

Based on my understanding, the users upload the word 2007 files into database and download them with your asp.net application. When you try to open the file that is downloaded, you get the error message above. If I have misunderstood you, please feel free to let me know.

To better understand your issue, could you please confirm the following information:

When we write the file to the client, we should make sure that file doesn't contain HTML-tag. We can add Response.End() right after writing and stop further processing of the ASP.NET page. For more information, seehttp://blog.benday.com/archive/2005/08/09/2705.aspx.


Yes, you have summarized what I am doing correctly.

I have been looking into this in more detail, and after using Hex Workshop, I compared the original file and the streamed file. The streamed file contained 1 extra byte.

I reviewed my code and found that the size of the file in the database was the same as the original, so I reviewed my code in the download.aspx code-behind (the file that streams the requested document).

I had read about this earlier, so I knew not to have any HTML in the aspx.

However, it seems that the Content-Length of the file is zero-based, so when I was setting Content-Length to equal the value I had in the database, an extra byte was being added.

When I set Content-Length to equal the size of the file (in bytes) - 1, I no longer received the error.

Ex:

Dim File As New EO.Library.File(CInt(Request("file")))
Dim _BinaryLength = (CInt(File.Binary.Length) - 1)

Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Disposition", "attachment;filename=" & File.Name)
Response.AddHeader("Content-Length", CInt(_BinaryLength))
Response.ContentType = File.ContentType
Response.BinaryWrite(File.Binary)
Response.End()

Thursday, March 22, 2012

OleDbConnection Security Exception

I wish to allow ASP.NET pages to make connections to Access databases using
OleDBConnection.

The only way I have been able to make this functionality work has been to
set:

Unrestricted="true"

In the <PermissionSet> property applying to ASP.NET pages, otherwise I get:

Security Exception
Description: The application attempted to perform an operation not allowed
by the security policy. To grant this application the required permission
please contact your system administrator or change the application's trust
level in the configuration file.

Exception Details: System.Security.SecurityException: Request failed.

I do not wish to run ASP.NET pages in an unrestricted security policy, I
would be grateful if you could inform me which permissions I must grant to
allow the use of OleDBConnection. I have granted unrestricted
OleDbPermission, FileIOPermission and RegistryPermission but to no avail.Nothing you can do about it.

ASP.NET requires Full Trust in order to allow OleDb, Odbc and OracleClient connections.

From the docs:

"This class is intended for future use when the .NET Framework Data Provider for OLE DB is enabled for partial trust scenarios. The .NET Framework Data Provider for OLE DB currently requires FullTrust permission. At present, using the OleDbPermission class has no effect."

Regards,
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

--
Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
Nothing you can do about it.

ASP.NET requires Full Trust in order to allow OleDb, Odbc and OracleClient connections.

From the docs:

"This class is intended for future use when the .NET Framework Data Provider for OLE DB is enabled for partial trust scenarios. The .NET Framework Data Provider for OLE DB currently requires FullTrust permission. At present, using the OleDbPermission class has no effect."

Regards,
Wim Hollebrandse
http://www.wimdows.net
http://www.wimdows.com

--
Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/