1

パラメータfile1で画像をURIにアップロードする必要があるコードがあります。しかし、コードは機能しません。画像がアップロードされないのはなぜですか?

これが私のコードです:

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)
    {

    }
}
4

2 に答える 2

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 はそれらを受け取るときに + 文字をスペースに変換します。

于 2013-11-01T14:06:03.657 に答える