如何在Excel VBA中调用file.io API上传文件并转译C#代码
嘿,我来帮你把C#的实现转成能在Excel VBA里运行的file.io API调用代码!刚好我之前折腾过类似的需求,咱们直接上干货,分上传和下载两部分说:
VBA调用file.io API实现文件上传与下载
一、文件上传代码
这部分对应你给出的curl -F "file=@test.txt" https://file.io命令,核心是构造multipart/form-data格式的POST请求,VBA里我们用MSXML2.XMLHTTP来实现:
Sub UploadFileToFileIO() Dim http As Object Dim fileContent As Byte() Dim boundary As String Dim postData As String Dim fileName As String Dim filePath As String ' 替换成你要上传的文件路径 filePath = "C:\test.txt" fileName = Dir(filePath) ' 读取文件为二进制字节数组(避免编码问题) Open filePath For Binary As #1 ReDim fileContent(LOF(1) - 1) Get #1, , fileContent Close #1 ' 初始化HTTP请求对象 Set http = CreateObject("MSXML2.XMLHTTP") ' 生成唯一的multipart边界(分隔请求体的不同部分) boundary = "----FileIOBoundary" & Format(Now, "YYYYMMDDHHMMSSFFF") ' 配置请求头和请求地址 http.Open "POST", "https://file.io", False http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary ' 构造请求体的头部部分 postData = "--" & boundary & vbCrLf postData = postData & "Content-Disposition: form-data; name=""file""; filename=""" & fileName & """" & vbCrLf postData = postData & "Content-Type: application/octet-stream" & vbCrLf & vbCrLf ' 把文本头部转成字节,和文件字节拼接成完整请求体 Dim postDataBytes As Byte() postDataBytes = StrConv(postData, vbFromUnicode) Dim endMarker As Byte() endMarker = StrConv(vbCrLf & "--" & boundary & "--" & vbCrLf, vbFromUnicode) ' 合并所有字节数据 Dim fullRequestData() As Byte ReDim fullRequestData(UBound(postDataBytes) + UBound(fileContent) + UBound(endMarker)) Call CopyMemory(fullRequestData(0), postDataBytes(0), UBound(postDataBytes) + 1) Call CopyMemory(fullRequestData(UBound(postDataBytes) + 1), fileContent(0), UBound(fileContent) + 1) Call CopyMemory(fullRequestData(UBound(postDataBytes) + UBound(fileContent) + 2), endMarker(0), UBound(endMarker) + 1) ' 发送请求并处理响应 http.send fullRequestData If http.Status = 200 Then MsgBox "上传成功!返回结果:" & vbCrLf & http.responseText ' 如果要解析JSON提取链接/密钥,推荐用VBA-JSON库 ' 示例: ' Dim json As Object ' Set json = JsonConverter.ParseJson(http.responseText) ' MsgBox "下载链接:" & json("link") & vbCrLf & "文件密钥:" & json("key") Else MsgBox "上传失败!状态码:" & http.Status & vbCrLf & "响应信息:" & http.responseText End If Set http = Nothing End Sub ' 用于高效复制字节数组的API声明(64位Excel用PtrSafe) Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
上传代码关键说明:
- 用字节数组处理文件内容:避免文本编码导致的文件损坏,尤其是非文本文件(比如图片、压缩包)。
- 自定义
boundary:必须是请求体中唯一的字符串,这里用时间戳保证唯一性。 CopyMemory:比循环赋值字节数组高效得多,适合处理大文件。
二、文件下载代码
对应你给出的curl https://file.io/2ojE41命令,直接发起GET请求保存响应内容即可:
Sub DownloadFileFromFileIO() Dim http As Object Dim downloadUrl As String Dim saveFilePath As String Dim fileBytes As Byte() ' 替换成你的file.io下载链接 downloadUrl = "https://file.io/2ojE41" ' 替换成你要保存的文件路径 saveFilePath = "C:\downloaded_test.txt" Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", downloadUrl, False http.send If http.Status = 200 Then ' 把响应的二进制内容写入文件 fileBytes = http.responseBody Open saveFilePath For Binary As #1 Put #1, , fileBytes Close #1 MsgBox "文件已成功保存到:" & saveFilePath Else MsgBox "下载失败!状态码:" & http.Status & vbCrLf & "响应信息:" & http.responseText End If Set http = Nothing End Sub
三、注意事项
- 宏安全设置:Excel可能会阻止外部API请求,需要在「文件>选项>信任中心>信任中心设置>宏设置」里调整为「启用所有宏」(或者添加信任位置)。
- JSON解析:如果要批量处理上传结果,推荐使用VBA-JSON库(直接搜索就能找到),可以轻松解析返回的JSON数据。
- 大文件处理:上面的代码是同步请求,大文件上传可能会导致Excel假死,需要的话可以改成异步请求(设置
http.Open的第三个参数为True,并绑定OnReadyStateChange事件)。 - 过期时间自定义:上传时可以在URL后加参数,比如
https://file.io?expiry=7d设置7天过期,支持的格式有1h(1小时)、30d(30天)等。
内容的提问来源于stack exchange,提问作者Nicola
相关产品推荐
相关产品推荐

