0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ETロボコン2023 C# Windowsアプリでロボコンスナップをアップロードする

Posted at

ETロボコン2023でロボコンスナップをダウンロードする側を作ったついでに
アップロード側も作ったので公開します。

private async void button1_Click(object sender, EventArgs e)
{
    HttpClient client = new HttpClient();
    string uri = "http://192.168.100.1/snap?id=1";     // 画像のアップロード チームID:1の例
    string path = @"LA.png";        // アップロードするファイル名

    try
    {
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            byte[] buf = new byte[fs.Length];
            fs.Read(buf, 0, (int)fs.Length);
            var content = new ByteArrayContent(buf);
            content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

            var response = await client.PostAsync(uri, content);

            if (response.StatusCode != HttpStatusCode.Created)
            {
                MessageBox.Show("競技システムへの画像アップロードに失敗しました. StatusCode=" + response.StatusCode.ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("競技システムと通信できません.\n" + ex.Message);
    }
}

急な投稿で雑ですが参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?