使用POST上传图像(Windows 8)

时间:2013-09-11 16:15:49

标签: c#

我有代码必须使用参数file1将图片上传到网址。但代码不起作用。为什么图片无法上传?

这是我的代码:

public void Upload
{
    string oauthUrl = "http://MY_Uri";
    HttpClient theAuthClient = new HttpClient();

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, oauthUrl);
    StorageFolder storageFolder = KnownFolders.PicturesLibrary;
    StorageFile sf = await storageFolder.GetFileAsync("ss.png");
    IBuffer buffer = await FileIO.ReadBufferAsync(sf);
    byte[] fileData = buffer.ToArray();
    Encoding encoding = Encoding.GetEncoding("Windows-1252");
    string text = encoding.GetString(fileData, 0, fileData.Length);
    string content = @"file1=" + text + "";

    txt.Text = content;
    StorageFolder storageFolder2 = KnownFolders.PicturesLibrary;
    StorageFile sampleFile = await storageFolder2.CreateFileAsync("sample.txt");
    await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "" + text + "");

    request.Method = HttpMethod.Post;
    request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));
    request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

    try
    {
        HttpResponseMessage response = await theAuthClient.SendAsync(request);
        handleResponse(response);
    }
    catch (HttpRequestException hre)
    {

    }
}
public async void handleResponse(HttpResponseMessage response)
{
    string content = await response.Content.ReadAsStringAsync();
    Account account = JsonConvert.DeserializeObject<Account>(content);
    if (content != null)
    {

    }
}

2 个答案:

答案 0 :(得分:1)

最近回答了类似的问题。文件内容在请求的正文中发送。

  1. 删除你的行:“request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));”
  2. 将Content-Type的标题更改为“image / png”或“application / octet-stream”。
  3. 以这种方式将您的字节数组写入请求(imb是您图像的byte []):

    using (Stream os = request.GetRequestStream()) { os.Write(imb, 0, imb.Length); }

  4. 指向类似问题的链接:A previous answer

答案 1 :(得分:0)

这是我用来在Windows 8应用程序中将图像上传到我的PHP服务器的代码

            string serverUrl = "http://www.mywebsite.com/receiveImage.php";

            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                IBuffer buffer = await FileIO.ReadBufferAsync(file);
                byte[] fileData = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(buffer.ToArray()).Replace("+","%2B"));
                byte[] prefix = System.Text.Encoding.UTF8.GetBytes("ImageData=");
                byte[] combinedData = new byte[fileData.Length + prefix.Length];
                System.Buffer.BlockCopy(prefix, 0, combinedData, 0, prefix.Length);
                System.Buffer.BlockCopy(fileData, 0, combinedData, prefix.Length, fileData.Length);

                Stream requestStream = await webRequest.GetRequestStreamAsync();
                requestStream.Write(combinedData, 0, combinedData.Length);


                // Get the response from the server.
                WebResponse response = await webRequest.GetResponseAsync();
                StreamReader requestReader = new StreamReader(response.GetResponseStream());
                String webResponse = requestReader.ReadToEnd();
            }
            catch (Exception ex)
            {

            }

Replace("+","%2B")很重要。没有那个+当PHP接收它们时,字符被转换为空格。