LEADTOOLS Support
General
General Questions
Invalid File Format using HttpPostedFile InputStream in ASP.NET 2.0
This topic and its replies were posted before the current version of LEADTOOLS was released and may no longer be applicable.
#1
Posted
:
Tuesday, June 26, 2007 7:53:30 AM(UTC)
Groups: Registered
Posts: 2
Hi
I need to be able to grab an image (jpeg) from an HTPP post save the original as well as a thumbnail of it. If I open the image from a file it works fins so I know the codecs is working fine. It also works correctly if I use the HttpPostedFile.SaveAs() on the InputStream.
When I try to convert to a stream and call the RasterCodecs.Load() method while passing in the stream I get the "Invalid file format" exception. I'm guessing that this may have to do with the contenttype or encoding or something but I am not sure.
If there is another way to do this in ASP.NET 2.0 that is great too. I am receiving an HTTP post from a Flash application so I don't have control over the Encoding types. I don't suspect this to be an issue because as I mentioned I can accept the post and write the file with the SaveAs() method fine.
I would greatly appreciate any help. Here is a code sample of what I am trying to do:
private void Upload()
{
HttpFileCollection files;
files = Page.Request.Files;
for (int index = 0; index < files.AllKeys.Length; index++)
{
HttpPostedFile pf = files[index];
int lastPos = pf.FileName.LastIndexOf('\\');
string[] fileName = pf.FileName.Substring(++lastPos).Split(new char[]{'.'});
string name = fileName[0];
string extension = fileName[1];
string path = MapPath("../images/");
Stream stream = pf.InputStream;
byte[]input = new byte[pf.ContentLength];
stream.Read(input, 0, pf.ContentLength);
//Check the file type through the extension
if (extension == "jpg" || extension =="jpeg" ||
extension == "gif" || extension == "png" ||
extension == "tiff")
{
Save(name, extension, path, stream, pf.ContentLength);
}
}
}
public void Save(string name, string extension, string path, Stream file, int size)
{
RasterCodecs.Startup();
RasterCodecs codecs = new RasterCodecs();
// Get the file information
//CodecsImageInfo info = codecs.GetInformation(file, true);
// Load the first page only - This fails
RasterImage image = codecs.Load(file);
//This works
//RasterImage image = codecs.Load("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg");
//Save the large format file to disk
codecs.Save(image, path + name + "." + extension, RasterImageFormat.Jpeg, 24);
//Resample and resize the preview for easy viewing
SizeCommand command = new SizeCommand();
command.Width = 128;
command.Height = 128;
command.Flags = RasterSizeFlags.Resample;
command.Run(image);
// Save the preview to disk
codecs.Save(image, path + name + "_preview." + extension, RasterImageFormat.Jpeg, 24);
// Clean Up
image.Dispose();
RasterCodecs.Shutdown();
}
#2
Posted
:
Wednesday, June 27, 2007 3:58:18 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
Did you seek to the beginning before performing load or save
operation in the memory stream?
#3
Posted
:
Wednesday, June 27, 2007 7:44:36 AM(UTC)
Groups: Registered
Posts: 2
Thanks for pointing me in the right direction. It had to do with not getting my stream built correctly.
All I did was put the HttpPostedFile.Inputstream into a Stream object.
Stream stream = pf.InputStream
I guess that allows it to buffer it in correctly? I had problems if I just tried to use the InputStream directly. Easy solution :)
#4
Posted
:
Tuesday, September 28, 2010 9:55:42 AM(UTC)
Groups: Registered
Posts: 1
Hi, I also had this problem, but the solution that ended up working for me was just slightly different so I wanted to share.
"Stream stream = pf.InputStream;" did not work for me; I still got the dreaded "Invalid File Format" error.
Here's the line of code I ended up with:
MemoryStream s = new MemoryStream(fuUpload.FileBytes, false);
I'm not sure why the difference, but I hope this can help save time for others!
#5
Posted
:
Wednesday, December 22, 2010 12:50:31 PM(UTC)
Groups: Registered
Posts: 6
The various RasterCodecs methods that use a stream (RasterCodecs.GetInformation/Load/Save), requires the stream to be seakable (Stream.CanSeek is true).
LEADTOOLS Support
General
General Questions
Invalid File Format using HttpPostedFile InputStream in ASP.NET 2.0
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.