Hi Xamarin Forum
can I get a little help I want to upload my multiple image to a folder in my web server can some body help me here is my current codes
XAML
<ScrollView>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="CREATE A POST" Margin="9,20,0,0" FontFamily="Raleway-Bold" FontAttributes="Bold" FontSize="16"></Label>
</StackLayout>
<Label Text="Title" FontFamily="Raleway-Black" Margin="20,0,20,0"></Label>
<controls:CustomEntry x:Name="HouseName" TextColor="Black" Margin="20,0,20,0" BackgroundColor="Transparent" FontAttributes="Bold" WidthRequest="200"/>
<StackLayout Orientation="Horizontal">
<StackLayout>
<Label Text="Year Built" FontFamily="Raleway-Black" Margin="20,0,20,0"></Label>
<controls:CustomEntry x:Name="yearbuiltentry" TextColor="Black" Margin="20,0,0,0" BackgroundColor="Transparent" FontAttributes="Bold" WidthRequest="150"/>
</StackLayout>
<StackLayout>
<Label Text="Price" FontFamily="Raleway-Black" Margin="20,0,10,0" HorizontalOptions="End"></Label>
<controls:CustomEntry x:Name="HousePrice" TextColor="Black" Margin="20,0,0,0" BackgroundColor="Transparent" FontAttributes="Bold" WidthRequest="190"/>
</StackLayout>
</StackLayout>
<Label Text="Location" FontFamily="Raleway-Black" Margin="20,0,20,0"></Label>
<controls:CustomEntry x:Name="HouseLocation" TextColor="Black" Margin="20,0,20,0" BackgroundColor="Transparent" FontAttributes="Bold" WidthRequest="200"/>
<Label Text="Description" FontFamily="Raleway-Black" Margin="20,0,20,0"></Label>
<controls:CustomEntry x:Name="postContent" TextColor="Black" Margin="20,0,20,0" BackgroundColor="Transparent" FontAttributes="Bold" WidthRequest="200"/>
<Label Text="IMAGES" FontAttributes="Bold" FontSize="16" FontFamily="Raleway-Bold" Margin="20,0,20,0"/>
<StackLayout Orientation="Horizontal" HeightRequest="100" Margin="20,0,0,0">
<flv:FlowListView FlowColumnCount="3" x:Name="listItems"
SeparatorVisibility="None"
HasUnevenRows="false" RowHeight="300">
<flv:FlowListView.FlowColumnTemplate>
<DataTemplate >
<ffimageloading:CachedImage DownsampleToViewSize="true" x:Name="ImageContainer" AbsoluteLayout.LayoutFlags="All" HeightRequest="300" AbsoluteLayout.LayoutBounds="0,0,1,1" Source="{Binding .}" Aspect="AspectFill" HorizontalOptions="FillAndExpand">
</ffimageloading:CachedImage>
</DataTemplate>
</flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>
<Button TextColor="White" BackgroundColor="Gray" Text="+" Clicked="Handle_Clicked" Margin="5,0,10,0"/>
</StackLayout>
<controls:CustomEntry x:Name="FloorArea" TextColor="Black" Margin="20,10,20,10" BackgroundColor="Transparent" Placeholder="Size in Sq Ft" FontAttributes="Bold" WidthRequest="200"/>
<Label Text="LIVING SPACE" FontFamily="Raleway-Black" Margin="20,0,10,0" FontAttributes="Bold"></Label>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Image Source="Bedroom.png" WidthRequest="50" HeightRequest="50"/>
<Label Text="Bedroom" FontFamily="Raleway-Black"/>
<Label BindingContext="{x:Reference inc_stepper}" Text="{Binding Value}" x:Name="Bedroom"/>
<Stepper Minimum="0" Maximum="4" x:Name="inc_stepper" Increment="1" StyleClass="stepperClass" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Image Source="Bath.png" WidthRequest="50" HeightRequest="50"/>
<Label Text="Baths" FontFamily="Raleway-Black"/>
<Label BindingContext="{x:Reference inc_stepper2}" Text="{Binding Value}" x:Name="BathToilet" />
<Stepper Minimum="0" Maximum="4" x:Name="inc_stepper2" Increment="1" Margin="100,0,0,0" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Image Source="Floors.png" WidthRequest="50" HeightRequest="50"/>
<Label Text="Floors" FontFamily="Raleway-Black"/>
<Label BindingContext="{x:Reference inc_stepper3}" Text="{Binding Value}" x:Name="Storey" />
<Stepper Minimum="0" Maximum="4" x:Name="inc_stepper3" Increment="1" Margin="100,0,0,0" />
</StackLayout>
</StackLayout>
<Label Text="AVAILABLE FOR" FontFamily="Raleway-Black" Margin="20,0,10,0" FontAttributes="Bold"></Label>
<Button Text="UPLOAD" Clicked="Upload_File"/>
</StackLayout>
</ScrollView>
CodeBehind
public partial class PostCreationPage : ContentPage
{
//public List postslist;
//string json;
List _images = new List();
public PostCreationPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", (s, images) =>
{
listItems.FlowItemsSource = images;
_images = images;
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<App, List<string>>(this, "ImagesSelected");
}
private MediaFile _mediaFile;
async void Handle_Clicked(object sender, System.EventArgs e)
{
// await DependencyService.Get<IMediaService>().OpenGallery();
//_mediaFile.GetStream();
//var memoryStream = new MemoryStream();
//if (Device.RuntimePlatform == Device.iOS)
//{
// //If the image is modified (drawings, etc) by the users, you will need to change the delivery mode to HighQualityFormat.
// bool imageModifiedWithDrawings = false;
// if (imageModifiedWithDrawings)
// {
// await GMMultiImagePicker.Current.PickMultiImage(true);
// }
// else
// {
// await GMMultiImagePicker.Current.PickMultiImage();
// }
// MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS");
// MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectediOS", (s, images) =>
// {
// //If we have selected images, put them into the carousel view.
// if (images.Count > 0)
// {
// listItems.ItemsSource = images;
// }
// });
//}
//If we are on Android, call IMediaService.
if (Device.RuntimePlatform == Device.Android)
{
DependencyService.Get<IMediaService>().OpenGallery();
MessagingCenter.Unsubscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid");
MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelectedAndroid", (s, images) =>
{
//If we have selected images, put them into the carousel view.
if (images.Count > 0)
{
listItems.ItemsSource = images;
}
});
}
}
private List<string> CompressAllImages(List<string> totalImages)
{
int displayCount = 1;
int totalCount = totalImages.Count;
List<string> compressedImages = new List<string>();
foreach (string path in totalImages)
{
compressedImages.Add(DependencyService.Get<ICompressImages>().CompressImage(path));
displayCount++;
}
return compressedImages;
}
private async void Upload_File(object sender, EventArgs e)
{
List<string> imagePaths = _images;
string userfirstname = App.FirstName;
var page = new LoadingPage();
var content = new MultipartFormDataContent();
var imageName = Path.GetFileName(imagePaths.ToString());
await PopupNavigation.Instance.PushAsync(page);
var httpClient = new HttpClient();
var Absolutepath_1 = "http://167.86.109.234:8087/Uploads";
Pipeline databaseConnect = new Pipeline();
try
{
SqlCommand sqlInsert = new SqlCommand("INSERT INTO tablename(House_Name, Post_Content, Photo, Photo_2, Photo_3, Photo_4, Location, Floor_Area, Price, Bedroom, Toilet_And_Bath, Uploader, Date_Uploaded) " +
"VALUES(@HouseName, @PostContent, @Photo, @Photo2, @Photo3, @Photo4, @Location, @Price, @Floor, @Bedroom, @TB, @Uploader, @DateUploaded)", databaseConnect.connectDB());
{
sqlInsert.Parameters.Add("@Photo", SqlDbType.VarChar);
sqlInsert.Parameters.Add("@Photo2", SqlDbType.VarChar);
sqlInsert.Parameters.Add("@Photo3", SqlDbType.VarChar);
sqlInsert.Parameters.Add("@Photo4", SqlDbType.VarChar);
foreach (var value in imagePaths)
{
string extensionName = ".jpg";
string pathNames = Absolutepath_1 + "/" + Path.GetFileName(value) + extensionName;
sqlInsert.Parameters["@Photo"].Value = pathNames;
sqlInsert.Parameters["@Photo2"].Value = pathNames;
sqlInsert.Parameters["@Photo3"].Value = pathNames;
sqlInsert.Parameters["@Photo4"].Value = pathNames;
}
sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
sqlInsert.Parameters.AddWithValue("@Location", HouseLocation.Text);
sqlInsert.Parameters.AddWithValue("@Price", HousePrice.Text);
sqlInsert.Parameters.AddWithValue("@Floor", FloorArea.Text);
sqlInsert.Parameters.AddWithValue("@Bedroom", Bedroom.Text);
sqlInsert.Parameters.AddWithValue("@HouseName", HouseName.Text);
sqlInsert.Parameters.AddWithValue("@TB", BathToilet.Text);
sqlInsert.Parameters.AddWithValue("@Storey", Storey.Text);
sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
int i = sqlInsert.ExecuteNonQuery();
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
await DisplayAlert("Success", "Uploaded to Database.", "OK");
}
}
catch (Exception ex)
{
}
}
internal static StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
Name = "\"files\"",
FileName = "\"" + fileName + "\""
};
return fileContent;
}
}
Thanks in advance