質問するログイン新規登録

質問編集履歴

4

追記

2021/03/22 01:30

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -76,4 +76,87 @@
76
76
  .Send postdata
77
77
  End With
78
78
  コード
79
+ ```
80
+
81
+ ###◆20210322追記(成功版)◆
82
+ バイナリファイルのアップロードのためにpostdataをバイナリ形式で送信する必要があったので、
83
+ 下記URLを参考にしてみたところ、元のファイル情報を保持したままファイルのアップロードができました。
84
+ [http://trash-area.com/archives/649](https://spirits.appirits.com/doruby/8783/)
85
+ [https://spirits.appirits.com/doruby/8783/](https://spirits.appirits.com/doruby/8783/)
86
+ ```vbs
87
+ filePath = "C:\Users\ユーザー\Desktop\test.pdf"
88
+ forderId = "1234567"
89
+ boundary = "------------------------ABCDEFG"
90
+
91
+ 'リクエストパラメータ生成
92
+ postData = SetPostData()
93
+
94
+ 'HTTPリクエストオブジェクト
95
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
96
+
97
+ With http
98
+ .Open "POST", "https://upload.box.com/api/2.0/files/content", False
99
+ .SetRequestHeader "Authorization", "Bearer " & token
100
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
101
+ .Send postData
102
+ End With
103
+
104
+ '-------------------------------------------------------
105
+ Function SetPostData()
106
+ adTypeBinary = 1
107
+ adTypeText = 2
108
+
109
+ ' ファイル名取得
110
+ Set fso = CreateObject("Scripting.FileSystemObject")
111
+ filename = fso.GetFileName(filepath)
112
+ Set fso = Nothing
113
+
114
+ ' アップロードファイルをバイナリ形式で読込
115
+ Set stream = CreateObject("ADODB.Stream")
116
+ stream.Type = adTypeBinary
117
+ stream.Open
118
+ stream.LoadFromFile filePath
119
+ fileContents = stream.Read
120
+ stream.Close
121
+
122
+ stream.Type = adTypeText
123
+ stream.Charset = "UTF-8"
124
+ stream.Open
125
+
126
+ ' バイナリデータの前まで
127
+ ChangeStreamType stream, adTypeText
128
+ params = ""
129
+ params = params & "--" & boundary & vbCrLf
130
+ params = params & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
131
+ params = params & vbCrLf
132
+ params = params & "{""name"":""" & filename & """, ""parent"":{""id"":""" & forderId & """}}" & vbCrLf
133
+ params = params & "--" & boundary & vbCrLf
134
+ params = params & "Content-Disposition: form-data; name=""file""; filename=" & filePath & vbCrLf
135
+ params = params & "Content-Type: application/octet-stream" & vbCrLf
136
+ params = params & vbCrLf
137
+ stream.WriteText params
138
+
139
+ ' バイナリデータ
140
+ ChangeStreamType stream, adTypeBinary
141
+ stream.Write fileContents
142
+
143
+ ' 最後
144
+ ChangeStreamType stream, adTypeText
145
+ stream.WriteText vbCrLf & "--" & boundary & "--" & vbCrLf
146
+
147
+ ChangeStreamType stream, adTypeBinary
148
+ stream.Position = 0
149
+ formData = stream.Read
150
+ stream.Close
151
+
152
+ SetPostData = formData
153
+ End Function
154
+
155
+ Function ChangeStreamType(stream, t)
156
+ p = stream.Position
157
+ stream.Position = 0
158
+ stream.Type = t
159
+ stream.Position = p
160
+ Set ChangeStreamType = stream
161
+ End Function
79
162
  ```

3

追記

2021/03/22 01:29

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -27,7 +27,7 @@
27
27
  .Send postdata
28
28
  End With
29
29
  ```
30
- ### ◆追記◆
30
+ ### ◆20210317追記◆
31
31
  boundary設定バージョン
32
32
  ```vbs
33
33
  postdata = ""
@@ -49,4 +49,31 @@
49
49
  .SetRequestHeader "Content-Type", "multipart/form-data; boundary=------------------------ABCDEFG"
50
50
  .Send postdata
51
51
  End With
52
+ ```
53
+
54
+ ###◆20210318追記◆
55
+ 下記の指定でアップロードに成功しました。
56
+ 20210317追記でのコードとほぼほぼ同じな気がしますが…
57
+ ```vbs
58
+ postdata = ""
59
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
60
+ postdata = postdata & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
61
+ postdata = postdata & vbCrLf
62
+ postdata = postdata & "{""name"":""api_upload.txt"", ""parent"":{""id"":""1234567""}}" & vbCrLf
63
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
64
+ postdata = postdata & "Content-Disposition: form-data; name=""file""; filename=""abc.txt""" & vbCrLf
65
+ postdata = postdata & "Content-Type: application/octet-stream" & vbCrLf
66
+ postdata = postdata & vbCrLf
67
+ postdata = postdata & "aiueo" & vbCrLf
68
+ postdata = postdata & "--------------------------ABCDEFG--"
69
+
70
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
71
+
72
+ With http
73
+ .Open method, requestUrl, False
74
+ .SetRequestHeader "Authorization", "Bearer " & token
75
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=------------------------ABCDEFG"
76
+ .Send postdata
77
+ End With
78
+ コード
52
79
  ```

2

修正

2021/03/18 13:18

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -27,7 +27,7 @@
27
27
  .Send postdata
28
28
  End With
29
29
  ```
30
- 20210317追記◆
30
+ ### ◆追記◆
31
31
  boundary設定バージョン
32
32
  ```vbs
33
33
  postdata = ""

1

追記

2021/03/17 10:12

投稿

coko1
coko1

スコア276

title CHANGED
File without changes
body CHANGED
@@ -26,4 +26,27 @@
26
26
  .SetRequestHeader "Content-Type", "multipart/form-data"
27
27
  .Send postdata
28
28
  End With
29
+ ```
30
+ ◆20210317追記◆
31
+ boundary設定バージョン
32
+ ```vbs
33
+ postdata = ""
34
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
35
+ postdata = postdata & "Content-Disposition: form-data; name=""attributes""" & vbCrLf
36
+ postdata = postdata & vbCrLf
37
+ postdata = postdata & "{""name"":""api_upload.txt"", ""parent"":{""id"":""123456789""}}" & vbCrLf
38
+ postdata = postdata & "--------------------------ABCDEFG" & vbCrLf
39
+ postdata = postdata & "Content-Disposition: form-data; name=""file""" & vbCrLf
40
+ postdata = postdata & vbCrLf
41
+ postdata = postdata & "C:\Users\ユーザー\Desktop\abc.txt" & vbCrLf
42
+ postdata = postdata & "--------------------------ABCDEFG--" & vbCrLf
43
+
44
+ Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
45
+
46
+ With http
47
+ .Open "POST", "https://upload.box.com/api/2.0/files/content", False
48
+ .SetRequestHeader "Authorization", "Bearer " & token
49
+ .SetRequestHeader "Content-Type", "multipart/form-data; boundary=------------------------ABCDEFG"
50
+ .Send postdata
51
+ End With
29
52
  ```