Hi xamarin forum
I have created uploading 4 photo to server however if I try to upload like 2 photos the app crashes anyway here is the code for uploading
Upload.cs
public partial class PostCreationPage : ContentPage
{
public List<t_Post> postslist;
string json;
public PostCreationPage()
{
InitializeComponent();
}
private MediaFile _mediaFile;
private MediaFile _mediaFile2;
private MediaFile _mediaFile3;
private MediaFile _mediaFile4;
List<string> _images = new List<string>();
private async void Upload_File(object sender, EventArgs e)
{
var content = new MultipartFormDataContent();
var content2 = new MultipartFormDataContent();
var content3 = new MultipartFormDataContent();
var content4 = new MultipartFormDataContent();
string userfirstname = App.FirstName;
var page = new LoadingPage();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
content2.Add(new StreamContent(_mediaFile2.GetStream()),
"\"file\"",
$"\"{_mediaFile2.Path}\"");
content3.Add(new StreamContent(_mediaFile3.GetStream()),
"\"file\"",
$"\"{_mediaFile3.Path}\"");
content4.Add(new StreamContent(_mediaFile4.GetStream()),
"\"file\"",
$"\"{_mediaFile4.Path}\"");
await PopupNavigation.Instance.PushAsync(page);
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://xxx.xx.xx.xxx:8087/api/Files/Uploads";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
var httpResponseMessage2 = await httpClient.PostAsync(uploadServiceBaseAddress, content2);
var Absolutepath_1 = "http://xxx.xx.xx.xxx:8087/Uploads";
var Absolutepath_2 = "http://xxx.xx.xx.xxx:8087/Uploads";
var Absolutepath_3 = "http://xxx.xx.xx.xxx:8087/Uploads";
var Absolutepath_4 = "http://xxx.xx.xx.xxx:8087/Uploads";
var pathToLabel = httpResponseMessage.Content.ReadAsStringAsync().ToString();
var pathToLabel2 = httpResponseMessage2.Content.ReadAsStringAsync().ToString();
string pathName = Path.GetFileName(_mediaFile.Path);
string pathName2 = Path.GetFileName(_mediaFile2.Path);
string pathName3 = Path.GetFileName(_mediaFile3.Path);
string pathName4 = Path.GetFileName(_mediaFile4.Path);
string pathNames = Absolutepath_1 + "/" + pathName;
string pathNames2 = Absolutepath_2 + "/" + pathName2;
string pathNames3 = Absolutepath_3 + "/" + pathName3;
string pathNames4 = Absolutepath_4 + "/" + pathName4;
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, Storey, Uploader, Date_Uploaded) " +
"VALUES(@HouseName, @PostContent, @Photo, @Photo2, @Photo3, @Photo4, @Location, @Price, @Floor, @Bedroom, @TB, @Storey, @Uploader, @DateUploaded)", databaseConnect.connectDB());
sqlInsert.Parameters.AddWithValue("@HouseName", HouseName.Text);
sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
sqlInsert.Parameters.AddWithValue("@Photo", pathNames);
sqlInsert.Parameters.AddWithValue("@Photo2", pathNames2);
sqlInsert.Parameters.AddWithValue("@Photo3", pathNames3);
sqlInsert.Parameters.AddWithValue("@Photo4", pathNames4);
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("@TB", BathToilet.Text);
sqlInsert.Parameters.AddWithValue("@Storey", Storey.Text);
int i = sqlInsert.ExecuteNonQuery();
databaseConnect.connectDB().Close();
await DisplayAlert("Success", "Uploaded to Database.", "OK");
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
}
catch (Exception ex)
{
}
}
}
}
Thanks in advance