|
|
Thanks, I read into that from the other Link.
It all makes more sense logically now, I think the problem that I didn't get was with the example I used, which was basically as I had seen in some same code when looking into something else. Misused I think
Illogical thoughts make me ill
|
|
|
|
|
Hi guys...
I have need some help cuz I'm totally lost of how I should figure this out...
My case is:
- Make a storage function, so text documents like PDF, docx, doc, can be stored in the Database (MSSQL08 express).
Functions:
- Show files in a ListView (check)
- Upload files selected from the client, and store them as varbinary(max) in the Database (check)
- Download the selected file on the ListView to the correct documenttype on the clients PC (uncheck)
My database table is quiet simple and look something like:
ID - int (Primary key)NOT NULL (is identity)
FileName - varchar(50)NOT NULL
FileContent - varbinary(MAX)NOT NULL
FileExt - varchar(8) NOT NULL
FileComment - varchar(MAX) NULL
So as you can see, I can with ease locate if the selected file got .doc og .pdf extension.
The storage of a file into the varbinary of byte[] works brilliant!
But I'm stucked at how do I download those selected files back to the client, so they look excatly the same as when the document got uploaded to the db?
I mainly have two types of documents stored:
- WORD files
- PDF files
Can anyone provide at codesnippet or anything, that shows how to make a varbinary file (with bytes) into a appropreated document on the client computer readable as WORD document and/or PDF?
|
|
|
|
|
Please refer to [this^] example for similar task.
What i can say is you can read bytes(varbinary) from SQL database and create a doc file, which you can open and preview to user. But, user cannot do on its own from database.
|
|
|
|
|
The view function is kinda cool, but my problem is, the code ain't ASP, but plain old winforms, and can't find a link between the asp view function and similar function for winforms?
|
|
|
|
|
How did you upload the files into your database in the first place? Assuming you used some form of reading the file from disk, then you need to reverse that process to write it back.
I must get a clever new signature for 2011.
|
|
|
|
|
I used FileStream like this:
public byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length;
buffer = new byte[length];
int count;
int sum = 0;
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count;
}
finally
{
fileStream.Close();
}
return buffer;
}
and stored the returned value byte[] as the varbinary into the Database... Try'd to figure the reverse way back, but didn't worked that well tbh, maybe because I didn't do it the write way, but when I stored it as .docx the file was 'damage' and when I store it as .txt there is just binary numbers.
|
|
|
|
|
Well you have not shown what you did to re-create the file so it is difficult to guess what may be the problem. However, if you merely reverse the process above: get the buffer from the database and write to the appropriate file, then it should be correct.
grmihel2 wrote: when I store it as .txt there is just binary numbers.
That is to be expected, .docx files are compressed XML.
I must get a clever new signature for 2011.
|
|
|
|
|
I try'd the following (had no clue where to start, so just gave it a try with BinaryWriter):
public void DownloadFile(string filSti, StoredFile fil)
{
byte[] file = WriteFile(filSti, fil);
BinaryWriter bw = new BinaryWriter(File.Open(@"d:\temp\doctest.docx", FileMode.OpenOrCreate));
bw.Write(file);
bw.Close();
}
Didn't worked that well... It created a .docx file of 32KB (the original was 14KB), and when I try to open it, Word says it cannot be open be cause it may be broken.. Any hint what to do? I have an idea then when I see the light of the end, it would be 'easy' to do the same operation for the PDF documents
|
|
|
|
|
grmihel2 wrote: had no clue where to start
The best thing I can suggest is to look at the classes and methods you are using to read the file, and find the opposite methods for writing. Are you sure that you are reading the file in binary and not using any form of character conversion and thus corrupting the file? Are you verifying every method call is successful and that the captured length of the file matches the original?
Also, what does this line do, I've not seen this call?
byte[] file = WriteFile(filSti, fil);
I must get a clever new signature for 2011.
|
|
|
|
|
Oh yes, sorry, the WriteFile function is following:
public byte[] WriteFile(string filePath, StoredFile file)
{
byte[] buffer = file.FilContent.ToArray();
FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
int length = (int)fileStream.Length;
buffer = new byte[length];
int count;
int sum = 0;
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count;
}
finally
{
fileStream.Close();
}
return buffer;
}
|
|
|
|
|
Why are you reading a file in your WriteFile() method? You should be getting the file content from your database and writing it to a disk file. The above code seems somewhat confusing.
I must get a clever new signature for 2011.
|
|
|
|
|
True, I can see that the Write method didn't make sense
I'm not really used to do these RW files programming, so I'm sorry for newbieness, but you have to learn it some how
Anyways, I have attempted to rewrite another write method like this:
public void WriteFile(string filePath, StoredFile file)
{
byte[] data = file.FilContent.ToArray();
FileStream fileStream = new FileStream(@"d:\temp\doctest.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
try
{
int length = (int)data.Length;
int buffer = 1000;
fileStream.SetLength(length);
fileStream.Seek(length - buffer, SeekOrigin.Begin);
fileStream.Write(data, 0, buffer);
fileStream.Flush();
}
finally
{
fileStream.Close();
}
}
The doctest.docx file is now 14KB as the original, so something seems right. But the file is still somehow corrupted, cuz I can't open the docx without an error message telling me the data in the Word document is wrong... Notice that the file.Filcontent is the varbinary value from the Database.
By the way, is it really so simple, that when I'm sure that its a .docx file I store in my DB as varbinary, I can just store it on local machine as a file with the same extension? No need of creating a Word application object and write it through that?
|
|
|
|
|
You seem to be getting confused with your file IO code:
int length = (int)data.Length;
int buffer = 1000;
fileStream.SetLength(length);
fileStream.Seek(length - buffer, SeekOrigin.Begin);
fileStream.Write(data, 0, data.Length);
fileStream.Flush();
I would suggest spending some time here[^] to get a better understanding of the FileStream class.
I must get a clever new signature for 2011.
|
|
|
|
|
Thanks a lot! That worked like a charm... I certainly need to study some more of the FileStreamers posibilities, thats a whole new world opening, but ain't that the beauty of coding, to explorer new posibilities?
Yet again, thnx for your expertise, it works brilliant now.
|
|
|
|
|
Happy to help, good luck!
I must get a clever new signature for 2011.
|
|
|
|
|
Hi,
I developed a desktop application using SQL Compact, used Entity Framework. Now I see, Entity Framework's connection string is not that simple, it stored many information in the connection string in app.config file. So, I do not touch that file. But what I see, the database folder is user |DataDirectory| which is actually Application.StartUpPath. When I distribute my program to end users, will they be able to run my application where the database is in the program files folder ? I know it was not a problem for XP. But when Vista came, the 'User Account Control' feature prevented accessing any files in Program Files folder. What about now ? Is that problem gone in Windows 7 ? IF NOT, then, how can I make the connection string dynamic (setting programmatically) for Entity framework ? Although I extremely like to use Application.StartUpPath to be my data store as it is easy for maintainability.
|
|
|
|
|
Your setup routine could add write privileges to the users, either to the startup directory or to the file(s) of your database.
|
|
|
|
|
The data should not be in the program directory.
|
|
|
|
|
HI all,
I am having a value in a encrypted format and the encryption and decryption technique used id 3DES.And also i am having a decryption key which is like "abcdefghiABCDEFGHI12345jKlMnopqrstuvWxYz67890ZLM" (i.e., length is 48).
Now can any one tell me how can i decrypt the encrypted value using decryption key.
And i am using the DNN database.
Thanks in advance.
modified on Thursday, February 17, 2011 11:41 PM
|
|
|
|
|
Decryption is the reversal of encryption: Whatever code you used to encrypt should provide the basis for your decryption routine.
There is an example of doing both here[^] which may help you, although it does some ancillary stuff you may not want. However, it should compare to your encryption code reasonably easily.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
According to Wikipedia (http://en.wikipedia.org/wiki/Triple_DES[^]), Triple DES uses three keys of 56 bits(! that is 7 bytes each, or 21 bytes in total). Something seems to be wrong with your 46 characters string. Perhaps a hash function is required to get the "real" key from your string?
|
|
|
|
|
Take a look at there[^] having same type question with some perfect solution.
hope it will works for you.
|
|
|
|
|
What is the mechanism to prevent other users to use class libraries developed and used in distributed UI application, neither as stand alone or by adding the reference to the UI application?
In native dll it is possible to handle licening e.g. in dll load event.
Чесноков
|
|
|
|
|
Use Friend Assemblies: Friend Assemblies at MSDN[^]
Your functions and classes can only be used by assemblies using a certain namespace, further protection is done using keys. This does not prevent ILDASM from disassembling though.
To prevent ILDASM from disassembling it, add this: [assembly: SuppressIldasmAttribute()][^]
|
|
|
|