async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var
file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(DBNAME);
byte[] fileBytes = null;
using (
var
stream = await file.OpenReadAsync())
{
fileBytes =
new
byte[stream.Size];
using (
var
reader =
new
DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(
"http://www.myserver.com/upload.php"
);
httpWebRequest.ContentType =
"multipart/form-data"
;
var
boundary =
"---------------------------"
+ DateTime.Now.Ticks.ToString(
"x"
);
httpWebRequest.Method =
"POST"
;
var
asyncResult = httpWebRequest.BeginGetRequestStream((ar) => { GetRequestStreamCallback(ar, fileBytes, boundary); }, httpWebRequest);
}
private
void GetRequestStreamCallback(IAsyncResult asynchronousResult, byte[] postData, string boundary)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
postStream.Write(postData, 0, postData.Length);
var
lineBreak =
"\r\n"
;
var
formData = $
"--{boundary}{lineBreak}Content-Disposition: form-data; name=\"userid\"{lineBreak}{lineBreak}SOME_ID{lineBreak}"
;
var
formDataBytes = System.Text.Encoding.UTF8.GetBytes(formData);
postStream.Write(formDataBytes, 0, formDataBytes.Length);
var
closingBoundary = $
"--{boundary}--{lineBreak}"
;
var
closingBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(closingBoundary);
postStream.Write(closingBoundaryBytes, 0, closingBoundaryBytes.Length);
postStream.Close();
var
asyncResult = request.BeginGetResponse(
new
AsyncCallback(GetResponseCallback), request);
}
private
void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
}