Hi Xamarin Forum,
Can someone help me about multiple uploading I have used James Montemagno's File uploading and image cropping but the docs is a little bit short to describe on how to make a multiple upload anyway here is my code that is currently using:
XAML
<StackLayout>
<Button Text="Pick image" Clicked="Select_Pic"/>
<Image x:Name="imageView" Aspect="AspectFit" />
<Controls:CustomEditor Placeholder="Write a Description.." x:Name="postContent" HeightRequest="100"/>
<Button Text="UPLOAD" Clicked="Upload_File"/>
</StackLayout>
Code Behind
public partial class PostCreationPage : ContentPage
{
public PostCreationPage ()
{
InitializeComponent ();
}
private MediaFile _mediaFile;
private string URL { get; set; }
ImageSource ImageSrc;
private async void Select_Pic(object sender, EventArgs e)
{
string[] fileTypes = null;
if(Device.RuntimePlatform == Device.Android)
{
fileTypes = new string[] { "image/jpeg" };
}
if (Device.RuntimePlatform == Device.iOS)
{
fileTypes = new string[] { "public.image" };
}
await CrossMedia.Current.Initialize();
if(!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "Image file type not support.", "OK");
return;
}
else
{
var mediaOption = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
_mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile == null) return;
imageView.Source = ImageSource.FromStream(() => _mediaFile.GetStream());
var memoryStream = new MemoryStream();
await _mediaFile.GetStream().CopyToAsync(memoryStream);
byte[] imageAsByte = memoryStream.ToArray();
await Navigation.PushModalAsync(new CropView(imageAsByte, Refresh));
}
}
private void Refresh()
{
try
{
if (App.CroppedImage != null)
{
var imageCrop = App.CroppedImage;
imageView.Source = ImageSource.FromStream(() => new MemoryStream(imageCrop));
var layout = new StackLayout();
var btn = new Button { Text = "Upload" };
CustomEditor CustomEditor = new Controls.CustomEditor { Placeholder = "Write a Post...", HeightRequest = 100, };
}
}
catch (Exception ex)
{
}
}
private async void Upload_File(object sender, EventArgs e)
{
if (_mediaFile == null)
{
await DisplayAlert("Error", "There was an error occured when trying to get your image.", "OK");
return;
}
else
{
UploadImage(_mediaFile.GetStream());
}
}
private async void UploadImage(Stream stream)
{
string[] fileTypes = null;
string filename = _mediaFile.Path;
string userfirstname = App.FirstName;
Pipeline databaseConnect = new Pipeline();
var imageCrop = App.CroppedImage;
BinaryReader br = new BinaryReader(stream);
byte[] bytes = br.ReadBytes((Int32)stream.Length);
var page = new LoadingPage();
await stream.ReadAsync(bytes, 0, (int)stream.Length);
try
{
await PopupNavigation.Instance.PushAsync(page);
SqlCommand sqlInsert = new SqlCommand("INSERT INTO tablename(Post_Content, Photo, Uploader, Date_Uploaded) " +
"VALUES(@PostContent, @Photo, @Uploader, @DateUploaded)", databaseConnect.connectDB());
sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
sqlInsert.Parameters.AddWithValue("@Photo", imageCrop);
sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
int i = sqlInsert.ExecuteNonQuery();
databaseConnect.connectDB().Close();
if (_mediaFile == null)
{
await DisplayAlert("Error", "There was an error when trying to get your image.", "OK");
return;
}
else
{
await DisplayAlert("Success", "Uploaded to Database.", "OK");
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
}
}
catch(Exception ex)
{
}
}
}
So how can I insert multiple uploading in this code
TIA