質問編集履歴

10

成功したコードに編集

2019/03/12 08:57

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- ※このコードはとりあえずテキストファイをアップしています。
13
+ Zuishin様のご指導のもと、このコードでエクセした!
14
14
 
15
15
  ```ここに言語を入力
16
16
 
@@ -50,6 +50,8 @@
50
50
 
51
51
 
52
52
 
53
+
54
+
53
55
  // 拡張子に対応したコンテンツタイプ
54
56
 
55
57
  string _type = Contents_type(filePath);
@@ -136,7 +138,7 @@
136
138
 
137
139
  // メッセージ追加
138
140
 
139
- postData = "--------------------------" + boundary + "\r\n";
141
+ postData = "\r\n--------------------------" + boundary + "\r\n";
140
142
 
141
143
  postData += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
142
144
 

9

コードを最新に変更

2019/03/12 08:57

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  ```ここに言語を入力
16
16
 
17
- static void Main(string[] args)
17
+ static void Main(string[] args)
18
18
 
19
19
  {
20
20
 
@@ -22,8 +22,6 @@
22
22
 
23
23
  }
24
24
 
25
-
26
-
27
25
  static void FileSendExe()
28
26
 
29
27
  {
@@ -42,13 +40,19 @@
42
40
 
43
41
  // ルーム番号
44
42
 
45
- string roomID = "00000000";
43
+ string roomID = "000000000";
46
44
 
47
45
 
48
46
 
49
47
  // トークン
50
48
 
51
- string apikey = "******************************";
49
+ string apikey = "**************************************";
50
+
51
+
52
+
53
+ // 拡張子に対応したコンテンツタイプ
54
+
55
+ string _type = Contents_type(filePath);
52
56
 
53
57
 
54
58
 
@@ -90,25 +94,15 @@
90
94
 
91
95
  //POST送信するデータを作成
92
96
 
93
- List<byte> vs = new List<byte>();
94
-
95
- byte[] v;
96
-
97
-
98
-
99
97
  string postData = "";
100
98
 
101
99
  postData = "--------------------------" + boundary + "\r\n" +
102
100
 
103
101
  "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
104
102
 
105
- "Content-Type: text/plain\r\n\r\n";
103
+ "Content-Type: " + _type + "\r\n\r\n";
106
-
107
-
108
-
109
- v = enc.GetBytes(postData);
104
+
110
-
111
- foreach (byte b in v) vs.Add(b);
105
+
112
106
 
113
107
 
114
108
 
@@ -134,31 +128,31 @@
134
128
 
135
129
 
136
130
 
137
- foreach (byte b in readData) vs.Add(b);
131
+ var _postData = enc.GetBytes(postData).ToList();
132
+
133
+ _postData.AddRange(readData);
138
134
 
139
135
 
140
136
 
141
137
  // メッセージ追加
142
138
 
143
- //postData += "\r\n--------------------------" + boundary + "\r\n"; メッセージ部分はとりあえず退避
139
+ postData = "--------------------------" + boundary + "\r\n";
144
-
140
+
145
- //postData += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
141
+ postData += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
146
-
142
+
147
- //postData += "I attached comment to chat.\r\n";
143
+ postData += "I attached comment to chat.\r\n";
148
-
144
+
149
- postData = "\r\n--------------------------" + boundary + "--";
145
+ postData += "--------------------------" + boundary + "--";
150
-
151
-
152
-
146
+
147
+
148
+
153
- v = enc.GetBytes(postData);
149
+ _postData.AddRange(enc.GetBytes(postData));
154
-
155
- foreach (byte b in v) vs.Add(b);
156
150
 
157
151
 
158
152
 
159
153
  // 送信バイナリデータの長さを指定
160
154
 
161
- req.ContentLength = vs.Count();
155
+ req.ContentLength = _postData.Count();
162
156
 
163
157
  // トークン設定
164
158
 
@@ -172,7 +166,11 @@
172
166
 
173
167
  // POSTで書込み
174
168
 
169
+ //reqStream.Write(_postData, 0, _postData.Length);
170
+
175
- foreach (byte b in vs) reqStream.WriteByte(b);
171
+ reqStream.Write(_postData.ToArray(), 0, _postData.Count);
172
+
173
+
176
174
 
177
175
 
178
176
 

8

コード修正

2019/03/12 08:52

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -140,13 +140,13 @@
140
140
 
141
141
  // メッセージ追加
142
142
 
143
- //postData += "--------------------------" + boundary + "\r\n"; メッセージ部分はとりあえず退避
143
+ //postData += "\r\n--------------------------" + boundary + "\r\n"; メッセージ部分はとりあえず退避
144
144
 
145
145
  //postData += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
146
146
 
147
147
  //postData += "I attached comment to chat.\r\n";
148
148
 
149
- postData = "--------------------------" + boundary + "--";
149
+ postData = "\r\n--------------------------" + boundary + "--";
150
150
 
151
151
 
152
152
 

7

コードを変更

2019/03/12 08:29

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -138,12 +138,6 @@
138
138
 
139
139
 
140
140
 
141
- // バイナリ文字列を追加
142
-
143
- postData += BitConverter.ToString(readData).Replace("-", string.Empty) + "\r\n";
144
-
145
-
146
-
147
141
  // メッセージ追加
148
142
 
149
143
  //postData += "--------------------------" + boundary + "\r\n"; メッセージ部分はとりあえず退避

6

コード編集

2019/03/12 07:39

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -90,6 +90,12 @@
90
90
 
91
91
  //POST送信するデータを作成
92
92
 
93
+ List<byte> vs = new List<byte>();
94
+
95
+ byte[] v;
96
+
97
+
98
+
93
99
  string postData = "";
94
100
 
95
101
  postData = "--------------------------" + boundary + "\r\n" +
@@ -100,6 +106,12 @@
100
106
 
101
107
 
102
108
 
109
+ v = enc.GetBytes(postData);
110
+
111
+ foreach (byte b in v) vs.Add(b);
112
+
113
+
114
+
103
115
  // 送信するファイルを開く
104
116
 
105
117
  System.IO.FileStream fs = new System.IO.FileStream(
@@ -122,6 +134,10 @@
122
134
 
123
135
 
124
136
 
137
+ foreach (byte b in readData) vs.Add(b);
138
+
139
+
140
+
125
141
  // バイナリ文字列を追加
126
142
 
127
143
  postData += BitConverter.ToString(readData).Replace("-", string.Empty) + "\r\n";
@@ -136,17 +152,19 @@
136
152
 
137
153
  //postData += "I attached comment to chat.\r\n";
138
154
 
139
- postData += "--------------------------" + boundary + "--";
155
+ postData = "--------------------------" + boundary + "--";
140
-
141
-
142
-
143
- // 送信データをバイナリ化
156
+
144
-
157
+
158
+
145
- byte[] _postData = enc.GetBytes(postData);
159
+ v = enc.GetBytes(postData);
160
+
161
+ foreach (byte b in v) vs.Add(b);
162
+
163
+
146
164
 
147
165
  // 送信バイナリデータの長さを指定
148
166
 
149
- req.ContentLength = _postData.Length;
167
+ req.ContentLength = vs.Count();
150
168
 
151
169
  // トークン設定
152
170
 
@@ -160,9 +178,7 @@
160
178
 
161
179
  // POSTで書込み
162
180
 
163
- //reqStream.Write(_postData, 0, _postData.Length);
164
-
165
- foreach (byte _b in _postData) reqStream.WriteByte(_b);
181
+ foreach (byte b in vs) reqStream.WriteByte(b);
166
182
 
167
183
 
168
184
 
@@ -191,29 +207,3 @@
191
207
  }
192
208
 
193
209
  ```
194
-
195
-
196
-
197
- 試しに下記の部分をエクセルではなくテキストファイルで上げてみました。
198
-
199
- // テストファイル
200
-
201
- string filePath = @"C:\Users****\Desktop\test.xlsx";
202
-
203
- のエクセルファイルを [あいうえお] と記載した test.txt のテキストファイルでアップしたところ
204
-
205
- アップロード後のテキストファイルに、16進数のバイナリデータが入力されていました。。。
206
-
207
-
208
-
209
- ということは、仕様の通り
210
-
211
- "Content-Type: application/vnd.ms-excel\r\n\r\n";
212
-
213
- の下にバイナリを記載してもファイルに復元されずに、テキストファイルとして
214
-
215
- 中身にバイナリ内容を文字列で記載しているだけなのかなと。。。
216
-
217
-
218
-
219
- どうすればファイルデータを渡せるでしょうか。

5

余計なコード削除

2019/03/12 07:37

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -52,12 +52,6 @@
52
52
 
53
53
 
54
54
 
55
- // 拡張子に対応したコンテンツタイプ
56
-
57
- string _type = Contents_type(filePath);
58
-
59
-
60
-
61
55
  // 文字コードを指定する
62
56
 
63
57
  var enc = Encoding.GetEncoding("UTF-8");
@@ -102,7 +96,7 @@
102
96
 
103
97
  "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
104
98
 
105
- "Content-Type: " + _type + "\r\n\r\n";
99
+ "Content-Type: text/plain\r\n\r\n";
106
100
 
107
101
 
108
102
 

4

コードを変更

2019/03/12 06:47

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -10,9 +10,11 @@
10
10
 
11
11
 
12
12
 
13
+ ※このコードはとりあえずテキストファイルをアップしています。
14
+
13
15
  ```ここに言語を入力
14
16
 
15
- static void Main(string[] args)
17
+ static void Main(string[] args)
16
18
 
17
19
  {
18
20
 
@@ -22,31 +24,37 @@
22
24
 
23
25
 
24
26
 
25
- static void FileSendExe()
27
+ static void FileSendExe()
26
28
 
27
29
  {
28
30
 
29
31
  // テストファイル
30
32
 
31
- string filePath = @"C:\Users****\Desktop\test.xlsx"; ※仮です。
33
+ string filePath = @"デスクトップ\test.txt";
32
34
 
33
35
 
34
36
 
35
37
  // 表示する添付ファイル名
36
38
 
37
- string filename = "test.xlsx";
39
+ string filename = "test.txt";
38
40
 
39
41
 
40
42
 
41
43
  // ルーム番号
42
44
 
43
- string roomID = "0000000"; ※仮です。
45
+ string roomID = "00000000";
44
46
 
45
47
 
46
48
 
47
49
  // トークン
48
50
 
49
- string apikey = "**************************"; ※仮です。
51
+ string apikey = "******************************";
52
+
53
+
54
+
55
+ // 拡張子に対応したコンテンツタイプ
56
+
57
+ string _type = Contents_type(filePath);
50
58
 
51
59
 
52
60
 
@@ -94,7 +102,7 @@
94
102
 
95
103
  "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
96
104
 
97
- "Content-Type: application/vnd.ms-excel\r\n\r\n";
105
+ "Content-Type: " + _type + "\r\n\r\n";
98
106
 
99
107
 
100
108
 
@@ -108,9 +116,11 @@
108
116
 
109
117
  System.IO.FileAccess.Read);
110
118
 
119
+ // 読取準備
120
+
111
121
  System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
112
122
 
113
- // ファイルをバイナリ読取
123
+ // ファイルをバイナリ
114
124
 
115
125
  byte[] readData = br.ReadBytes((Int32)fs.Length);
116
126
 
@@ -122,6 +132,16 @@
122
132
 
123
133
  postData += BitConverter.ToString(readData).Replace("-", string.Empty) + "\r\n";
124
134
 
135
+
136
+
137
+ // メッセージ追加
138
+
139
+ //postData += "--------------------------" + boundary + "\r\n"; メッセージ部分はとりあえず退避
140
+
141
+ //postData += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
142
+
143
+ //postData += "I attached comment to chat.\r\n";
144
+
125
145
  postData += "--------------------------" + boundary + "--";
126
146
 
127
147
 
@@ -146,7 +166,11 @@
146
166
 
147
167
  // POSTで書込み
148
168
 
149
- reqStream.Write(_postData, 0, _postData.Length);
169
+ //reqStream.Write(_postData, 0, _postData.Length);
170
+
171
+ foreach (byte _b in _postData) reqStream.WriteByte(_b);
172
+
173
+
150
174
 
151
175
  // 書込みストリームを閉じる
152
176
 

3

追記

2019/03/12 05:59

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -173,3 +173,29 @@
173
173
  }
174
174
 
175
175
  ```
176
+
177
+
178
+
179
+ 試しに下記の部分をエクセルではなくテキストファイルで上げてみました。
180
+
181
+ // テストファイル
182
+
183
+ string filePath = @"C:\Users****\Desktop\test.xlsx";
184
+
185
+ のエクセルファイルを [あいうえお] と記載した test.txt のテキストファイルでアップしたところ
186
+
187
+ アップロード後のテキストファイルに、16進数のバイナリデータが入力されていました。。。
188
+
189
+
190
+
191
+ ということは、仕様の通り
192
+
193
+ "Content-Type: application/vnd.ms-excel\r\n\r\n";
194
+
195
+ の下にバイナリを記載してもファイルに復元されずに、テキストファイルとして
196
+
197
+ 中身にバイナリ内容を文字列で記載しているだけなのかなと。。。
198
+
199
+
200
+
201
+ どうすればファイルデータを渡せるでしょうか。

2

string _type = Contents_type(filePath); を削除しました。

2019/03/11 10:47

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -47,12 +47,6 @@
47
47
  // トークン
48
48
 
49
49
  string apikey = "**************************"; ※仮です。
50
-
51
-
52
-
53
- // 拡張子に対応したコンテンツタイプ
54
-
55
- string _type = Contents_type(filePath);
56
50
 
57
51
 
58
52
 

1

Content-Type: application/vnd.ms-excel に修正しました。

2019/03/11 10:34

投稿

Com
Com

スコア30

test CHANGED
File without changes
test CHANGED
@@ -100,7 +100,7 @@
100
100
 
101
101
  "Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
102
102
 
103
- "Content-Type: " + _type + "\r\n\r\n";
103
+ "Content-Type: application/vnd.ms-excel\r\n\r\n";
104
104
 
105
105
 
106
106