473,778 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing System.IO.Strea m derived from a DIME attachment to a file

It seems incredulous to me that it is so difficult to write the
contents of a memory stream to a file. I'm certain that I'm missing
something simple.

I am retrieving a memory stream from a DIME attachment:

MyDimeService svc = new MyDimeService() ;
svc.CreateDimed Image();
if (svc.ResponseSo apContext.Attac hments.Count == 1)
{
System.IO.Strea m dimeStream =
svc.ResponseSoa pContext.Attach ments[0].Stream;
SaveStreamToFil e(ref dimeStream);
}

In SaveStreamToFil e I'm simply attempting to write the stream to a
file similar to the way it's done in the MSDN help:

private void SaveStreamToFil e(ref System.IO.Strea m pStreamToSave)
{
System.IO.FileS tream fs = new
System.IO.FileS tream(@"c:\imag es\new\new.gif" ,
System.IO.FileM ode.Create);
System.IO.Binar yWriter bw = new System.IO.Binar yWriter(fs);

for (int i=0; i<=pStreamToSav e.Length; i++)
{
bw.Write(pStrea mToSave.ReadByt e());
}
bw.Close();
fs.Close();
}
Unfortunately, my file ends up four times the size of the original.

There has to be some standard method of creating a binary file from a
stream.

I've looked through the newsgroups and really haven't found anything
other than the method I'm doing.

Any help would be greatly appreciated.

Thanks...
Nov 16 '05 #1
4 6385
Scott, Killer of all Ninjas <dm****@hotmail .com> wrote:
It seems incredulous to me that it is so difficult to write the
contents of a memory stream to a file. I'm certain that I'm missing
something simple.

I am retrieving a memory stream from a DIME attachment:

MyDimeService svc = new MyDimeService() ;
svc.CreateDimed Image();
if (svc.ResponseSo apContext.Attac hments.Count == 1)
{
System.IO.Strea m dimeStream =
svc.ResponseSoa pContext.Attach ments[0].Stream;
SaveStreamToFil e(ref dimeStream);
}
Why are you passing the stream reference by reference?
In SaveStreamToFil e I'm simply attempting to write the stream to a
file similar to the way it's done in the MSDN help:

private void SaveStreamToFil e(ref System.IO.Strea m pStreamToSave)
{
System.IO.FileS tream fs = new
System.IO.FileS tream(@"c:\imag es\new\new.gif" ,
System.IO.FileM ode.Create);
System.IO.Binar yWriter bw = new System.IO.Binar yWriter(fs);

for (int i=0; i<=pStreamToSav e.Length; i++)
{
bw.Write(pStrea mToSave.ReadByt e());
}
bw.Close();
fs.Close();
}
Using a using block or two would be much better - and there's actually
no need for a BinaryWriter either.
Unfortunately, my file ends up four times the size of the original.
Yes - that's because you're calling BinaryWriter.Wr ite(int) for each
byte of the original.
There has to be some standard method of creating a binary file from a
stream.

I've looked through the newsgroups and really haven't found anything
other than the method I'm doing.


The simplest way is to read chunks of data and write them out. It's
also more efficient than the above, it'll get the right length, it
doesn't require the Length property of the stream (which you may have
in this case, but not in others, such as with network streams), and it
also still closes the file stream even if an exception is thrown.
Here's some sample code:

using (FileStream stream = new FileStream (filename, FileMode.Create ))
{
byte[] buffer = new byte[32768];
int chunkLength;

while((chunkLen gth=pStreamToSa ve.Read(buffer, 0,buffer.Length )) > 0)
{
stream.Write (buffer, 0, chunkLength);
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Excellent.

Simple, straight forward. I was able to find something similar to
this shortly after I left my message, but there was a lot more code
involved.

That being said, I started by passing a .gif file through (for easy
verification of the destination file), and in either case, after I
create the file from the attachment, it is no longer a valid gif file.

I have tried it with the chunk size you had, with a much smaller one
(256), with a REALLY small one (1) and the one directly from the DIME
attachment (pDimgeAttachme nt.ChunkSize). None of this made any
difference in the destination file.

The file sizes vary slightly:

Source File Info: 124,742 bytes actual, 126,976 bytes on disk
Destination File Info: 123,961 bytes actual, 126,976 bytes on disk

This is how it comes across regardless of the chunk size

Any ideas? Thanks.

my new function:

private string SaveAttachment(
DimeAttachment pDimgeAttachmen t)
{

string sFileName = @"c:\images\new \" +
pDimgeAttachmen t.Id.ToString() .Split(':')[1].ToString();

using (FileStream stream = new FileStream (sFileName,
FileMode.Create ))
{
byte[] buffer = new byte[pDimeAttachment .ChunkSize];
int chunkLength;

while((chunkLen gth = pDimgeAttachmen t.Stream.Read(b uffer, 0,
buffer.Length)) > 0)
stream.Write (buffer, 0, chunkLength);
}
return sFileName;
}
-
DM

Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message >
Using a using block or two would be much better - and there's actually
no need for a BinaryWriter either.
Unfortunately, my file ends up four times the size of the original.


Yes - that's because you're calling BinaryWriter.Wr ite(int) for each
byte of the original.
There has to be some standard method of creating a binary file from a
stream.

I've looked through the newsgroups and really haven't found anything
other than the method I'm doing.


The simplest way is to read chunks of data and write them out. It's
also more efficient than the above, it'll get the right length, it
doesn't require the Length property of the stream (which you may have
in this case, but not in others, such as with network streams), and it
also still closes the file stream even if an exception is thrown.
Here's some sample code:

using (FileStream stream = new FileStream (filename, FileMode.Create ))
{
byte[] buffer = new byte[32768];
int chunkLength;

while((chunkLen gth=pStreamToSa ve.Read(buffer, 0,buffer.Length )) > 0)
{
stream.Write (buffer, 0, chunkLength);
}
}

Nov 16 '05 #3
Scott, Killer of all Ninjas <dm****@hotmail .com> wrote:
Excellent.

Simple, straight forward. I was able to find something similar to
this shortly after I left my message, but there was a lot more code
involved.

That being said, I started by passing a .gif file through (for easy
verification of the destination file), and in either case, after I
create the file from the attachment, it is no longer a valid gif file.

I have tried it with the chunk size you had, with a much smaller one
(256), with a REALLY small one (1) and the one directly from the DIME
attachment (pDimgeAttachme nt.ChunkSize). None of this made any
difference in the destination file.

The file sizes vary slightly:

Source File Info: 124,742 bytes actual, 126,976 bytes on disk
Destination File Info: 123,961 bytes actual, 126,976 bytes on disk

This is how it comes across regardless of the chunk size

Any ideas? Thanks.


Hmm... no immediate ideas come to mind. Are you sure that the DIME
attachment has been created correctly to start with? (I'm afraid I have
very little knowledge of DIME myself.)

If you have a look at the original and final files with a binary
editor, does anything obvious strike you in terms of where they differ?
For instance, is there a load of header information at the start or
anything like that?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote in message news:<MP******* *************** **@msnews.micro soft.com>...
Hmm... no immediate ideas come to mind. Are you sure that the DIME
attachment has been created correctly to start with? (I'm afraid I have
very little knowledge of DIME myself.)

If you have a look at the original and final files with a binary
editor, does anything obvious strike you in terms of where they differ?
For instance, is there a load of header information at the start or
anything like that?


I looked at the two files and the "new" file is actually completely
missing the GIF89a header. I'm not sure what the problem with it is.

I've actually looked into using BITS (Background Intelligent Transfer
Service) to do the file transfer since DIME has been such a hassle,
but BITS seems even more complicated, and the client has to have BITS
1.5 installed in order to do uploads.

Anyone with DIME Attachment experience know why my attachment is
missing the first ~496 bytes?
-
dm
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1150
by: Vaylor Trucks | last post by:
I have a web service written in ASP.NET using the .NET frame v1.1.4322 and Web Service Enhancements 1.0 SP1. This web service has 2 nearly identical methods. That is, the SOAP XML is the same from one method to the next, the only difference being that one accepts the transfer of an attachment using DIME and the other does not. When the non-DIME version of the method is invoked everything works properly. When the DIME version is...
1
2210
by: Drew | last post by:
I am having trouble with receiving an attachment via WSDL/Dime. I have a service that I am connected to on a Single Board Computer running linux. The Single Board contains a text file that is transmitted through DIME on the Single Boards WSDL. I can get all the reads from the WSDL, but cannot receive the attachment. Here is the error I receive on my C# ASP.net app -
6
13304
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileID.ToString() + ".pdf");
2
1550
by: Sven Thorsen | last post by:
I have a Web Service with a method that handles file uploads. The file is sent as a DIME attachment to the SOAP request. The post is successful, the file is received and the correct response is sent back to the client. So there seems to be nothing wrong at all, except: This is UNBELIEVEABLY slow! My last tests were with a 4MB file. The tests took between 30 and 40 seconds total. The web service code finished its work in 750-790...
0
1148
by: Vaylor Trucks | last post by:
Using the .NET framework and WSE 1.0 SP1, I have built a web service which receives data from a web service client and saves it to disk in a text file, and it works with no problem. However, when I modify the web service to accept an attachment and save it to disk as well (resulting in 2 files saved - txt file and attachment) then any special characters in the text file (such as the N~ combination used in Spanish) appear as 2 question...
0
1339
by: munish | last post by:
Hi, I am trying to send a tiff file which contains multiple pages/frames as a dime attachment. When I am saving the file on the server only the first page of image gets saved. Any Ideasv how to pass Multipage tiff
1
2195
by: Matt B | last post by:
Hi all, This problem is close to driving me insane. Basically, I'm trying to use DIME to add an attachment to a SOAP message and send it to the web server. The WSE 2.0 toolkit is installed, which allows for DIME to be used and everything is set up correctly as far as I know. My code is:
5
3260
by: =?Utf-8?B?SmFrb2IgTGl0aG5lcg==?= | last post by:
I have never sent attachment with webservices. Yesterday I got the challenge to redesign my solution that sends large XML structures to a Java webservice. The reason was that the Java SOAP implementation had problems consuming too large files that way. They encountered a memory leak in the parser that made the whole mainframe environment to crash! So the decided they should go for DIME attachments instead. I use VB.Net in VS2005.
0
1348
by: mazdotnet | last post by:
Hi all, I'm trying to integrate a third party library that would parse an excel file and allows us to make changes to. Once I update the appropriate cells, I want to allow my users to download the modified file. It works by IWorkbook book = NativeExcel.Factory.OpenWorkbook(this.MapPath(FileName));
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.