This project is read-only.

Uploading Jpeg Image from Windows Phone 8 via BlogImporter.asmx

Topics: ASP.NET 2.0
Feb 10, 2013 at 5:05 AM
Edited Feb 10, 2013 at 5:11 AM
Hello Everyone!

I'm so excited I can now create a Blogengine Post from my WP8. But I'm not quite done yet.

I need to upload my camera image as part of my post. I know the images belong in the \App_data\files\Year\Month folder. But how do I get the jpeg file uploaded to that folder?

Anybody has a sample code for BlogImageImageHandler?

Please help.

Feb 24, 2013 at 4:25 PM
I have tried serializing the image using the following code:
                // Set the position of the stream back to start
                e.ImageStream.Seek(0, SeekOrigin.Begin);

                MemoryStream s = new MemoryStream();
                s.Position = 0;
                var sr = new StreamReader(s);
                importPost.CameraImageString = sr.ReadToEnd();
When I single-step my code and evaluate importPost.CameraImageString the xml string looks fine. So I think my issue is with working with the Async operation of the Windows Phone. Another reason I think the issue is with the Async operation is that when I don't set the importPost.CameraImageString (which is a large string almost 2MB in size) the web service works and a post is created.

Has anyone here used the async and wait functions in WP8?

Feb 24, 2013 at 7:16 PM

Hi rtur!

Thanks for the reply. I was able to extend the BlogImporter.asmx to add a string that represents the serialized image in xml.

I think my issue has to do with using an async method inside Windows Phone 8. My provider ( only supports up to 3.5 .Net so I’m forced to use a WCF consumer on an asmx web service.

This is really a WP8 question, I just posted it in BlogEngine hoping that someone else is using a WP8 client to post to the BlogEngine.

Thanks and enjoy your Sunday!

From: rtur [email removed]
Sent: Sunday, February 24, 2013 11:22 AM
Subject: Re: Uploading Files via BlogImporter.asmx [blogengine:432603]

From: rtur

Not sure how you trying to do this, but this is a code fro file upload in admin/FileManager/FileUpload.ashx

using System.IO;
using BlogEngine.Core;
using BlogEngine.Core.Providers;
//check rights
if (!Security.IsAuthorizedTo(Rights.CreateNewPosts))
    context.Response.Write("Access Denied");
var files = context.Request.Files;
if (files.Count == 0)
    context.Response.Write("0:Unknown file upload");
var file = files[0];
var dirName = string.Format("/{0}/{1}", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"));
var dir = BlogService.GetDirectory(dirName);  
var uFile = BlogService.UploadFile(file.InputStream, file.FileName, dir, true);
if (uFile.IsImage)
    var imgString = string.Format("", uFile.AsImage.ImageUrl);
    context.Response.Write(string.Format("1:{0}:{1}:{2}", imgString, uFile.Name, uFile.ParentDirectory.FullPath));
    var fileString = string.Format("


", uFile.FileDownloadPath, uFile.FileDescription);
    context.Response.Write(string.Format("1:{0}:{1}:{2}", fileString, uFile.Name, uFile.ParentDirectory.FullPath));
Mar 2, 2013 at 5:53 AM
Hi All!

I was able to fix my issue by creating a new public property named "CameraImageBlob" to the BlogImporter with a type of byte[ ].

Then on BlogImporter class I added this code:
            string _Folder = HttpContext.Current.Server.MapPath("~/App_Data/files/"); 
            Stream s = new MemoryStream();
            s.Position = 0;
            s.Write(import.CameraImageBlob, 0, import.CameraImageBlob.Length);
            Bitmap b = new Bitmap(s, true);
            Bitmap bm = new Bitmap(b);
            bm.Save(_Folder + nowString + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
The code above deserializes the byte [ ] to jpeg.

In my Windows Phone 8 code, I was able to serialize the jpeg to byte [ ] using this code:
                byte[] blob;
                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                using (Stream stream = isoStore.OpenFile(fileName, FileMode.Open))
                    blob = new byte[stream.Length];
                    int length =  await stream.ReadAsync(blob, 0, (int)stream.Length);
                    importPost.CameraImageBlob = blob;
So, there you have it. Hope it helps someone. Most of the code came from MSDN.