0

I'm downloading .TIFF images from the web via a WebService. I receive the images as byte[]. I want to download these images and I'm doing so with this code:

Response.ClearContent();
Response.ContentType = MimeType; // images/tiff
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Convert.ToString(FileName) + "\"");
byte[] FileContent = Convert.FromBase64String(getFileContentAsBase64String()); // Get the file content
Response.BinaryWrite((byte[])FileContent);
Response.Flush();

This is working as intended with one file. I have a case where I want to download multiple files at the time but displaying them as a single file. At the moment I'm using this code, but it is not working as intended. It's downloading the image, but only the first one. I'm not sure what happens with the 2nd and 3rd.

long PageNoLong = long.Parse(PageNo);  // Number of pages (files), in this case we have 3                  

Response.ClearContent();
Response.ContentType = MimeType;
string FileName = "filename.tiff";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Convert.ToString(FileName) + "\"");

//In this case, the loop loops three times, returning three byte[] which i write to the Response
for (int i = 1; i < PageNoLong+1; i++)
{                                
    byte[] FileContent = Convert.FromBase64String(getFileContentAsBase64String());                                                                                                 
    Response.BinaryWrite((byte[])FileContent);
}

Response.Flush();

How do I merge these three byte[] into a single .tiff file?

Marcus
  • 6,697
  • 11
  • 46
  • 89
  • 1
    So what's your question: how to perform multiple HTTP requests in succession? Then show all relevant code, you're probably not disposing the relevant objects. Or is your question how to concatenate byte arrays? In fact I think this code functions exactly as intended, it's appending the three TIFF files. You just can't do that and call it one TIFF AFAIK. – CodeCaster Feb 10 '17 at 15:43
  • 1
    Does the file size of the generated file match the number of received bytes in total? I'm a bit confused; it seems as if you are concatenating the received files into a single file; what is the goal? – Codor Feb 10 '17 at 15:43
  • 2
    Are you sure that you can just concatenate the binary contents of multiple TIFF images? I don't think that this will work, most likely only the first image will ever show this way. You probably need to actually combine the 3 images into a single new valid TIFF file. – bassfader Feb 10 '17 at 15:43
  • I would try and get a zip as a response, and download that zip and unpack it... – Callum Linington Feb 10 '17 at 15:44
  • @bassfader " You probably need to actually combine the 3 images into a single new valid TIFF file" - Thats what I am trying to do, unsuccessfully. – Marcus Feb 10 '17 at 15:44
  • 1
    Then all code you showed apart from `Response.BinaryWrite((byte[])FileContent)` is irrelevant. It's way easier to just save three TIFF files on disk, and concatenate them from a console application. See duplicate. – CodeCaster Feb 10 '17 at 15:46
  • It seems to be possible to use [`multipart` responses](http://stackoverflow.com/a/2332460/993547) too, but they seem to be unreliable as far as I can read. – Patrick Hofman Feb 10 '17 at 15:47

1 Answers1

-1

You would have to merge the images using some other means first then download the merged images. You can't just combine the multiple byte[]'s as that doesn't really combine the files.

Think about it, how are the TIFF files to be merged? One on top of the other? Beside each other? What is the size of the new image.

Check out the link below on how to merge images in c#

Merging two images in C#/.NET

Community
  • 1
  • 1
MikeS
  • 1,734
  • 1
  • 9
  • 13
  • You suggest to paste one image on the other? Not a real solution... – Patrick Hofman Feb 10 '17 at 15:45
  • Not really pasting them, he is asking "I want to download multiple files at the time but displaying them as a single file." Which I would assume means a merged image. You can't merge multiple images simply by combining the byte stream. I'm suggesting he needs to merge the images into a single image if he wants to "display them as a single file". Not sure my answer really warrants a down vote. – MikeS Feb 10 '17 at 15:48
  • See the link you propose as solution: it places one image on top of the other. – Patrick Hofman Feb 10 '17 at 15:48
  • And? He doesn't say how he wants to display them as a single file. One on top of the other is an option, is it not? – MikeS Feb 10 '17 at 15:49
  • I see what the other link proposed does, sets them up as a multi-page TIFF. The asker was not clear what he meant by 'display them as a single file' – MikeS Feb 10 '17 at 15:51