質問編集履歴
3
コードの内容に質問の内容と関係のない部分があったので削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -125,7 +125,7 @@
|
|
125
125
|
--96403343
|
126
126
|
Content-Disposition: form-data; name="title"
|
127
127
|
|
128
|
-
|
128
|
+
テスト
|
129
129
|
--96403343
|
130
130
|
Content-Disposition: form-data; name="user"
|
131
131
|
|
@@ -199,10 +199,10 @@
|
|
199
199
|
string pdffileName = Path.GetFileName(pdffilePath);
|
200
200
|
|
201
201
|
//パラメータ設定
|
202
|
-
string title = "
|
202
|
+
string title = "テスト";
|
203
|
-
string user = "test"; //
|
203
|
+
string user = "test"; //
|
204
204
|
int category = 1;
|
205
|
-
bool check = false; //
|
205
|
+
bool check = false; //
|
206
206
|
|
207
207
|
|
208
208
|
string tick = Environment.TickCount.ToString();
|
2
情報が不足していたため、詳細追加。
test
CHANGED
File without changes
|
test
CHANGED
@@ -95,3 +95,231 @@
|
|
95
95
|
}
|
96
96
|
}
|
97
97
|
```
|
98
|
+
|
99
|
+
|
100
|
+
### 追記
|
101
|
+
上手く行かない→
|
102
|
+
```ここに言語を入力
|
103
|
+
--boundary
|
104
|
+
Content-Disposition: form-data; name="フォーム1"; filename="ファイル1"
|
105
|
+
Content-Type: application/octet-stream
|
106
|
+
Content-Transfer-Encoding: binary
|
107
|
+
|
108
|
+
--boundary
|
109
|
+
Content-Disposition: form-data; name="フォーム2"; filename="ファイル2"
|
110
|
+
Content-Type: application/octet-stream
|
111
|
+
Content-Transfer-Encoding: binary
|
112
|
+
```
|
113
|
+
というようにヘッダーの内容を記述し、送信したときに。
|
114
|
+
フォーム1に指定のファイルがPOSTされることは確認できるのですが、フォーム2へのPOSTが確認できない。
|
115
|
+
フォーム2への送信処理がとばされているのかと思ったのですが、Fiddlerを使い、内容を確認したところそういうわけでは無さそうです。
|
116
|
+
|
117
|
+
Postmanで送信したときの内容とプログラムから送信したときの内容を見比べてみました。
|
118
|
+
同じcsvファイルをPOSTしているのは確認がしやすいようにです、ファイル形式が違うため上げることが出来ないなどはありません。
|
119
|
+
|
120
|
+
Postmanの方は末尾に"--"が付いていますが、これは処理に関係あるのでしょうか?
|
121
|
+
|
122
|
+
以下 プログラムから送信したときの内容
|
123
|
+
|
124
|
+
```ここに言語を入力
|
125
|
+
--96403343
|
126
|
+
Content-Disposition: form-data; name="title"
|
127
|
+
|
128
|
+
!テ!!!!!
|
129
|
+
--96403343
|
130
|
+
Content-Disposition: form-data; name="user"
|
131
|
+
|
132
|
+
test
|
133
|
+
--96403343
|
134
|
+
Content-Disposition: form-data; name="category"
|
135
|
+
|
136
|
+
1
|
137
|
+
--96403343
|
138
|
+
Content-Disposition: form-data; name="checked"
|
139
|
+
|
140
|
+
False
|
141
|
+
--96403343
|
142
|
+
Content-Disposition: form-data; name="pdf_file"; filename="Book1.csv"
|
143
|
+
Content-Type: application/octet-stream
|
144
|
+
Content-Transfer-Encoding: binary
|
145
|
+
|
146
|
+
11818818
|
147
|
+
18118
|
148
|
+
99999
|
149
|
+
|
150
|
+
--96403343
|
151
|
+
Content-Disposition: form-data; name="csv_file"; filename="Book1.csv"
|
152
|
+
Content-Type: application/octet-stream
|
153
|
+
Content-Transfer-Encoding: binary
|
154
|
+
|
155
|
+
11818818
|
156
|
+
18118
|
157
|
+
99999
|
158
|
+
|
159
|
+
--96403343
|
160
|
+
```
|
161
|
+
|
162
|
+
|
163
|
+
以下Postmanから送信したときの内容
|
164
|
+
```ここに言語を入力
|
165
|
+
----------------------------228829904362102449550779
|
166
|
+
Content-Disposition: form-data; name="title"
|
167
|
+
|
168
|
+
test114
|
169
|
+
----------------------------228829904362102449550779
|
170
|
+
Content-Disposition: form-data; name="csv_file"; filename="Book1.csv"
|
171
|
+
Content-Type: text/csv
|
172
|
+
|
173
|
+
11818818
|
174
|
+
18118
|
175
|
+
99999
|
176
|
+
|
177
|
+
----------------------------228829904362102449550779
|
178
|
+
Content-Disposition: form-data; name="pdf_file"; filename="Book1.csv"
|
179
|
+
Content-Type: text/csv
|
180
|
+
|
181
|
+
11818818
|
182
|
+
18118
|
183
|
+
99999
|
184
|
+
|
185
|
+
----------------------------228829904362102449550779--
|
186
|
+
```
|
187
|
+
|
188
|
+
コードの方も少し変えました
|
189
|
+
```ここに言語を入力
|
190
|
+
//送信先のURL
|
191
|
+
string url = "http://localhost:8000/api/filedata/";
|
192
|
+
|
193
|
+
//送信対象のCSVファイルのパスと名前
|
194
|
+
string filePath = "C:\\tmp_file\\nest1\\Book1.csv";
|
195
|
+
string fileName = Path.GetFileName(filePath);
|
196
|
+
|
197
|
+
//送信対象のPDFファイルのパスと名前
|
198
|
+
string pdffilePath = "C:\\tmp_file\\nest1\\Book1.csv";
|
199
|
+
string pdffileName = Path.GetFileName(pdffilePath);
|
200
|
+
|
201
|
+
//パラメータ設定
|
202
|
+
string title = "!テ!!!!!";
|
203
|
+
string user = "test"; // 公開先のUserName
|
204
|
+
int category = 1;
|
205
|
+
bool check = false; // 既読 未読
|
206
|
+
|
207
|
+
|
208
|
+
string tick = Environment.TickCount.ToString();
|
209
|
+
System.Text.Encoding enc = System.Text.Encoding.UTF8;
|
210
|
+
|
211
|
+
//WebRequestの生成
|
212
|
+
WebRequest req = WebRequest.Create(url);
|
213
|
+
req.Method = "POST";
|
214
|
+
req.ContentType = "multipart/form-data; boundary=" + tick;
|
215
|
+
|
216
|
+
byte[] boundary = enc.GetBytes("--" + tick);
|
217
|
+
byte[] crlf = enc.GetBytes("\r\n");
|
218
|
+
List<byte[]> headers = new List<byte[]>();
|
219
|
+
|
220
|
+
//全体のヘッダー
|
221
|
+
string postheader =
|
222
|
+
"--" + tick + "\r\n" +
|
223
|
+
"Content-Disposition: form-data; name=\"title\"\r\n\r\n" + title + "\r\n" +
|
224
|
+
"--" + tick + "\r\n" +
|
225
|
+
"Content-Disposition: form-data; name=\"user\"\r\n\r\n" + user + "\r\n" +
|
226
|
+
"--" + tick + "\r\n" +
|
227
|
+
"Content-Disposition: form-data; name=\"category\"\r\n\r\n" + category + "\r\n" +
|
228
|
+
"--" + tick + "\r\n" +
|
229
|
+
"Content-Disposition: form-data; name=\"checked\"\r\n\r\n" + check + "\r\n";
|
230
|
+
|
231
|
+
//ヘッダの作成とデータサイズの計算
|
232
|
+
long contentLen = 0;
|
233
|
+
|
234
|
+
contentLen += enc.GetBytes(postheader).Length;
|
235
|
+
|
236
|
+
string pdfheader = "Content-Disposition: form-data; name=\"pdf_file\"; filename=\"" + pdffileName + "\"\r\n" +
|
237
|
+
"Content-Type: text/csv\r\n\r\n";
|
238
|
+
|
239
|
+
contentLen += boundary.Length + crlf.Length + enc.GetBytes(pdfheader).Length + new FileInfo(pdffilePath).Length + crlf.Length;
|
240
|
+
|
241
|
+
string csvheader = "Content-Disposition: form-data; name=\"csv_file\"; filename=\"" + fileName + "\"\r\n" +
|
242
|
+
"Content-Type: text/csv\r\n\r\n" +
|
243
|
+
"--" + tick + "--\r\n";
|
244
|
+
|
245
|
+
contentLen += boundary.Length + crlf.Length + enc.GetBytes(csvheader).Length + new FileInfo(filePath).Length;// + crlf.Length;
|
246
|
+
|
247
|
+
//全体のデータサイズ
|
248
|
+
req.ContentLength = contentLen + boundary.Length;
|
249
|
+
|
250
|
+
|
251
|
+
|
252
|
+
//送信
|
253
|
+
using (Stream reqStream = req.GetRequestStream())
|
254
|
+
{
|
255
|
+
//全体のヘッダ
|
256
|
+
reqStream.Write(enc.GetBytes(postheader), 0, enc.GetBytes(postheader).Length);
|
257
|
+
|
258
|
+
using (FileStream pdfFs = new FileStream(pdffilePath, FileMode.Open, FileAccess.Read))
|
259
|
+
{
|
260
|
+
//pdf部ヘッダ
|
261
|
+
reqStream.Write(boundary, 0, boundary.Length);
|
262
|
+
reqStream.Write(crlf, 0, crlf.Length);
|
263
|
+
reqStream.Write(enc.GetBytes(pdfheader), 0, enc.GetBytes(pdfheader).Length);
|
264
|
+
|
265
|
+
//ファイル内容
|
266
|
+
byte[] pdfbuf = new byte[0x1000];
|
267
|
+
int pdfreadSize = 0;
|
268
|
+
while (true)
|
269
|
+
{
|
270
|
+
pdfreadSize = pdfFs.Read(pdfbuf, 0, pdfbuf.Length);
|
271
|
+
if (pdfreadSize == 0) break;
|
272
|
+
|
273
|
+
reqStream.Write(pdfbuf, 0, pdfreadSize);
|
274
|
+
}
|
275
|
+
|
276
|
+
reqStream.Write(crlf, 0, crlf.Length);
|
277
|
+
}
|
278
|
+
|
279
|
+
using (FileStream csvFs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
280
|
+
{
|
281
|
+
//csv部ヘッダ
|
282
|
+
reqStream.Write(boundary, 0, boundary.Length);
|
283
|
+
reqStream.Write(crlf, 0, crlf.Length);
|
284
|
+
reqStream.Write(enc.GetBytes(csvheader), 0, enc.GetBytes(csvheader).Length);
|
285
|
+
|
286
|
+
//ファイル内容
|
287
|
+
byte[] csvbuf = new byte[0x1000];
|
288
|
+
int csvreadSize = 0;
|
289
|
+
while (true)
|
290
|
+
{
|
291
|
+
csvreadSize = csvFs.Read(csvbuf, 0, csvbuf.Length);
|
292
|
+
if (csvreadSize == 0) break;
|
293
|
+
|
294
|
+
reqStream.Write(csvbuf, 0, csvreadSize);
|
295
|
+
}
|
296
|
+
|
297
|
+
reqStream.Write(crlf, 0, crlf.Length);
|
298
|
+
}
|
299
|
+
reqStream.Write(boundary, 0, boundary.Length);
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
|
1
コードに不備があった
test
CHANGED
File without changes
|
test
CHANGED
@@ -49,7 +49,7 @@
|
|
49
49
|
{
|
50
50
|
//ヘッダ
|
51
51
|
header = "--" + tick + "\r\n" +
|
52
|
-
"Content-Disposition: form-data; name=\"
|
52
|
+
"Content-Disposition: form-data; name=\"" + fileNames[i] + i.ToString() + "\"; filename=\"" + Path.GetFileName(files[i]) + "\"\r\n" +
|
53
53
|
"Content-Type: application/octet-stream\r\n" +
|
54
54
|
"Content-Transfer-Encoding: binary\r\n\r\n";
|
55
55
|
|