Saturday 11 October 2014

Upload Image to Server using Windows Store app

First the pick the image from the galley using File Picker.

FileOpenPicker imagePicker = new FileOpenPicker
                {
                    ViewMode = PickerViewMode.Thumbnail,
                    SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                    FileTypeFilter = { ".png", ".jpg" }
                };
                pickedImage = await imagePicker.PickSingleFileAsync();
                if (pickedImage != null)
                {
                    IRandomAccessStream displayStream = await pickedImage.OpenAsync(FileAccessMode.Read);
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(displayStream);
                    imageBytes = await byteImgConvert.GetBytesFromFile(pickedImage);
                    strImagename = pickedImage.Name;
                    addpicture.Source = bitmapImage;
                    popupAddPicture.IsOpen = true;
                    txtImg.Text = txtcontent;
                }



string serverUrl = "http://10.10.10.1/sampleapplication/imageupload/";

                    HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
                    webRequest.Method = "POST";
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    String webResponse="";
                    byte[] fileData = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(imageBytes.ToArray()).Replace("+", "%2B"));
                    byte[] prefix = System.Text.Encoding.UTF8.GetBytes("up_img=");
                    byte[] combinedData = new byte[fileData.Length + prefix.Length];
                    System.Buffer.BlockCopy(prefix, 0, combinedData, 0, prefix.Length);
                    System.Buffer.BlockCopy(fileData, 0, combinedData, prefix.Length, fileData.Length);
                    Stream requestStream = await webRequest.GetRequestStreamAsync();
                    requestStream.Write(combinedData, 0, combinedData.Length);
                    WebResponse response = await webRequest.GetResponseAsync();
                    StreamReader requestReader = new StreamReader(response.GetResponseStream());
                    webResponse = requestReader.ReadToEnd();
                    if (webResponse.Contains("success") == true)
                    {
                            MessageDialog uploadSuccess = new MessageDialog("Picture posted successfully");
                        await uploadSuccess.ShowAsync();
}

Windows Store App -Application Bar

<Page.BottomAppBar>
    <AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
        <Grid>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Style="{StaticResource EditAppBarButtonStyle}" Click="Button_Click"/>
                <Button Style="{StaticResource RemoveAppBarButtonStyle}" Click="Button_Click"/>
                <Button Style="{StaticResource AddAppBarButtonStyle}" Click="Button_Click"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Style="{StaticResource RefreshAppBarButtonStyle}" Click="Button_Click"/>
                <Button Style="{StaticResource HelpAppBarButtonStyle}" Click="Button_Click"/>
            </StackPanel>
        </Grid>
    </AppBar>
</Page.BottomAppBar>
App bar (Windows only)
A toolbar for displaying application-specific commands.