質問編集履歴

4

追記

2021/03/22 01:30

投稿

coko1
coko1

スコア276

test CHANGED
File without changes
test CHANGED
@@ -155,3 +155,169 @@
155
155
  コード
156
156
 
157
157
  ```
158
+
159
+
160
+
161
+ ###◆20210322追記(成功版)◆
162
+
163
+ バイナリファイルのアップロードのためにpostdataをバイナリ形式で送信する必要があったので、
164
+
165
+ 下記URLを参考にしてみたところ、元のファイル情報を保持したままファイルのアップロードができました。
166
+
167
+ [http://trash-area.com/archives/649](https://spirits.appirits.com/doruby/8783/)
168
+
169
+ [https://spirits.appirits.com/doruby/8783/](https://spirits.appirits.com/doruby/8783/)
170
+
171
+ ```vbs
172
+
173
+ filePath = "C:\Users\ユーザー\Desktop\test.pdf"
174
+
175
+ forderId = "1234567"
176
+
177
+ boundary = "------------------------ABCDEFG"
178
+
179
+
180
+
181
+ 'リクエストパラメータ生成
182
+
183
+ postData = SetPostData()
184
+
185
+
186
+
187
+ 'HTTPリクエストオブジェクト
188
+
189
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
190
+
191
+
192
+
193
+ With http
194
+
195
+ .Open "POST", "https://upload.box.com/api/2.0/files/content", False
196
+
197
+ .SetRequestHeader "Authorization", "Bearer " & token
198
+
199
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
200
+
201
+ .Send postData
202
+
203
+ End With
204
+
205
+
206
+
207
+ '-------------------------------------------------------
208
+
209
+ Function SetPostData()
210
+
211
+ adTypeBinary = 1
212
+
213
+ adTypeText = 2
214
+
215
+
216
+
217
+ ' ファイル名取得
218
+
219
+ Set fso = CreateObject("Scripting.FileSystemObject")
220
+
221
+ filename = fso.GetFileName(filepath)
222
+
223
+ Set fso = Nothing
224
+
225
+
226
+
227
+ ' アップロードファイルをバイナリ形式で読込
228
+
229
+ Set stream = CreateObject("ADODB.Stream")
230
+
231
+ stream.Type = adTypeBinary
232
+
233
+ stream.Open
234
+
235
+ stream.LoadFromFile filePath
236
+
237
+ fileContents = stream.Read
238
+
239
+ stream.Close
240
+
241
+
242
+
243
+ stream.Type = adTypeText
244
+
245
+ stream.Charset = "UTF-8"
246
+
247
+ stream.Open
248
+
249
+
250
+
251
+ ' バイナリデータの前まで
252
+
253
+ ChangeStreamType stream, adTypeText
254
+
255
+ params = ""
256
+
257
+ params = params & "--" & boundary & vbCrLf
258
+
259
+ params = params & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
260
+
261
+ params = params & vbCrLf
262
+
263
+ params = params & "{""name"":""" & filename & """, ""parent"":{""id"":""" & forderId & """}}" & vbCrLf
264
+
265
+ params = params & "--" & boundary & vbCrLf
266
+
267
+ params = params & "Content-Disposition: form-data; name=""file""; filename=" & filePath & vbCrLf
268
+
269
+ params = params & "Content-Type: application/octet-stream" & vbCrLf
270
+
271
+ params = params & vbCrLf
272
+
273
+ stream.WriteText params
274
+
275
+
276
+
277
+ ' バイナリデータ
278
+
279
+ ChangeStreamType stream, adTypeBinary
280
+
281
+ stream.Write fileContents
282
+
283
+
284
+
285
+ ' 最後
286
+
287
+ ChangeStreamType stream, adTypeText
288
+
289
+ stream.WriteText vbCrLf & "--" & boundary & "--" & vbCrLf
290
+
291
+
292
+
293
+ ChangeStreamType stream, adTypeBinary
294
+
295
+ stream.Position = 0
296
+
297
+ formData = stream.Read
298
+
299
+ stream.Close
300
+
301
+
302
+
303
+ SetPostData = formData
304
+
305
+ End Function
306
+
307
+
308
+
309
+ Function ChangeStreamType(stream, t)
310
+
311
+ p = stream.Position
312
+
313
+ stream.Position = 0
314
+
315
+ stream.Type = t
316
+
317
+ stream.Position = p
318
+
319
+ Set ChangeStreamType = stream
320
+
321
+ End Function
322
+
323
+ ```

3

追記

2021/03/22 01:29

投稿

coko1
coko1

スコア276

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  ```
58
58
 
59
- ### ◆追記◆
59
+ ### ◆20210317追記◆
60
60
 
61
61
  boundary設定バージョン
62
62
 
@@ -101,3 +101,57 @@
101
101
  End With
102
102
 
103
103
  ```
104
+
105
+
106
+
107
+ ###◆20210318追記◆
108
+
109
+ 下記の指定でアップロードに成功しました。
110
+
111
+ 20210317追記でのコードとほぼほぼ同じな気がしますが…
112
+
113
+ ```vbs
114
+
115
+ postdata = ""
116
+
117
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
118
+
119
+ postdata = postdata & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
120
+
121
+ postdata = postdata & vbCrLf
122
+
123
+ postdata = postdata & "{""name"":""api_upload.txt"", ""parent"":{""id"":""1234567""}}" & vbCrLf
124
+
125
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
126
+
127
+ postdata = postdata & "Content-Disposition: form-data; name=""file""; filename=""abc.txt""" & vbCrLf
128
+
129
+ postdata = postdata & "Content-Type: application/octet-stream" & vbCrLf
130
+
131
+ postdata = postdata & vbCrLf
132
+
133
+ postdata = postdata & "aiueo" & vbCrLf
134
+
135
+ postdata = postdata & "--------------------------ABCDEFG--"
136
+
137
+
138
+
139
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
140
+
141
+
142
+
143
+ With http
144
+
145
+ .Open method, requestUrl, False
146
+
147
+ .SetRequestHeader "Authorization", "Bearer " & token
148
+
149
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=------------------------ABCDEFG"
150
+
151
+ .Send postdata
152
+
153
+ End With
154
+
155
+ コード
156
+
157
+ ```

2

修正

2021/03/18 13:18

投稿

coko1
coko1

スコア276

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  ```
58
58
 
59
- 20210317追記◆
59
+ ### ◆追記◆
60
60
 
61
61
  boundary設定バージョン
62
62
 

1

追記

2021/03/17 10:12

投稿

coko1
coko1

スコア276

test CHANGED
File without changes
test CHANGED
@@ -55,3 +55,49 @@
55
55
  End With
56
56
 
57
57
  ```
58
+
59
+ ◆20210317追記◆
60
+
61
+ boundary設定バージョン
62
+
63
+ ```vbs
64
+
65
+ postdata = ""
66
+
67
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
68
+
69
+ postdata = postdata & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
70
+
71
+ postdata = postdata & vbCrLf
72
+
73
+ postdata = postdata & "{""name"":""api_upload.txt"", ""parent"":{""id"":""123456789""}}" & vbCrLf
74
+
75
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
76
+
77
+ postdata = postdata & "Content-Disposition: form-data; name=""file""" & vbCrLf
78
+
79
+ postdata = postdata & vbCrLf
80
+
81
+ postdata = postdata & "C:\Users\ユーザー\Desktop\abc.txt" & vbCrLf
82
+
83
+ postdata = postdata & "--------------------------ABCDEFG--" & vbCrLf
84
+
85
+
86
+
87
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
88
+
89
+
90
+
91
+ With http
92
+
93
+ .Open "POST", "https://upload.box.com/api/2.0/files/content", False
94
+
95
+ .SetRequestHeader "Authorization", "Bearer " & token
96
+
97
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=------------------------ABCDEFG"
98
+
99
+ .Send postdata
100
+
101
+ End With
102
+
103
+ ```