Windows Universal 8.1—上传文件到服务器
本文关键字:文件 服务器 Universal Windows | 更新日期: 2023-09-27 18:12:42
我有一个任务POST(上传照片)照片到服务器。我试着从"高级休息客户端"发布它,它工作得很好。状态200 OK,上传照片。发帖地址:http://xx.xx.xx.xx:8080/photo/api/upload.php?d=Multimedia/shared并添加了一个图像文件,文件名应为"files"。PFA @ https://i.stack.imgur.com/N8cv5.png
And here is what all I tried from my code
1. Background Uploader
path = "http://xx.xx.xx.xx:8080/photo/api/upload.php?d=Multimedia/shared";
Uri uri = new Uri(path, UriKind.Absolute);
BackgroundUploader uploader = new BackgroundUploader();
uploader.SetRequestHeader("files", file.Name);
UploadOperation upload = uploader.CreateUpload(uri, file);
await HandleUploadAsync(upload, true);
Not Working.
2. http POST method
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri(RequestUrl, UriKind.Absolute));
myRequest.Method = "POST";
myRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
writeMultipartObject_dictOfObj(postStream, postParam_dictOfObj);
postStream.Dispose();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
public void writeMultipartObject_dictOfObj(Stream stream,object data,string fileName)
{
StreamWriter writer = new StreamWriter(stream);
if (data != null)
{
foreach (var entry in data as Dictionary<string, object>)
{
WriteEntry(writer, entry.Key, entry.Value, fileName);
}
}
writer.Write(boundary);
writer.Flush();
}
private void WriteEntry(StreamWriter writer,string key, object value, string fileName)
{
Stream output = writer.BaseStream;
if (value != null)
{
writer.WriteLine(boundary);
if (value is byte[])
{
byte[] ba = value as byte[];
writer.WriteLine(@"Content-Dis-data; name=""{0}""; filename=""{1}""", key, fileName);
writer.WriteLine(@"Content-Type:application/octet-stream");
writer.WriteLine(@"Content-Length: " + ba.Length);
writer.WriteLine();
output.Write(ba, 0, ba.Length);
output.Flush();
writer.WriteLine();
}
else
{
writer.WriteLine(@"Content-Dis-data; name=""{0}""", key);
writer.WriteLine();
writer.WriteLine(value.ToString());
}
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
String Response = streamReader.ReadToEnd();
}
}
This also not working.
I tried with all the answers I got from Stack Overflow and MSDN blogs but couldn't find the Solution to my problem. So please help me resolve this issue or suggest any other options.
Thanks in Advance
我猜你没有以服务器期望的格式发送请求。我建议您尝试使用HttpMultipartFormDataContent
或MultipartFormDataContent
:
HttpMultipartFormDataContent multipartContent =
new HttpMultipartFormDataContent();
multipartContent.Add(
new HttpStreamContent(inputStream),
"myFile",
file.Name);
HttpResponseMessage response = await client.PostAsync(
uri,
multipartContent);
。净
Stream stream = await file.OpenStreamForReadAsync();
MultipartFormDataContent multipartContent =
new MultipartFormDataContent();
multipartContent.Add(
new StreamContent(stream),
"myFile",
file.Name);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(
uri,
multipartContent);
HttpMultipartFormDataContent multipartContent =
new HttpMultipartFormDataContent();
multipartContent.Add(
new HttpStreamContent(inputStream),
"myFile",
file.Name);
HttpResponseMessage response = await client.PostAsync(
uri,
multipartContent);
Stream stream = await file.OpenStreamForReadAsync();
MultipartFormDataContent multipartContent =
new MultipartFormDataContent();
multipartContent.Add(
new StreamContent(stream),
"myFile",
file.Name);
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(
uri,
multipartContent);
Is this Approach right ?
HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());
streamContent.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
streamContent.Headers.Add("Content-Length", stream.Length.ToString());
streamContent.Headers.Add("Content-Disposition", "form-data; name='"" + flKey + "'"; filename='"" + fleNm + "'"");
Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, uri);
request.Content = streamContent;
var httpClient = new Windows.Web.Http.HttpClient();
var cts = new CancellationTokenSource();
Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
String resp = response.ToString();