sharepoint - 通过 Visual Basic 脚本从 Sharepoint 下载文件
问题描述
我正在尝试通过 VBScript 从共享点下载文件。
不幸的是,我在下载 Excel 文件后收到以下错误消息:
“Excel 无法打开文件 'xxxxx.xlsx',因为文件格式或文件扩展名无效。验证文件没有损坏并且文件扩展名与文件格式匹配”
我的代码剪断如下:
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "https://company.sharepoint.com/:x:/r/teams/xxxxx/_layouts/15/Doc.aspx?sourcedoc=%7B5AA396CB-4711-4E73-AEC5-3CB8B6E174D3%7D&file=xxxx%20Automation%xxxx.xlsx&wdOrigin=OFFICECOM-WEB.START.REC&action=default&mobileredirect=true", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\temp\TestNow.xlsx", 2 '//overwrite
end with
解决方案
该方法没有错,但在处理来自 URL 的下载时,您应该始终检查响应状态代码以查看它是否有效,然后再继续。
调用后Send()
总是在这样的条件语句中包围响应;
If xHttp.Status = 200 Then 'Expecting a HTTP 200 OK response
With bStrm
.Type = 1 '//binary
.Open
.Write xHttp.responseBody
.SaveToFile "c:\temp\TestNow.xlsx", 2 '//overwrite
End With
Else
'Check the response body for details of the error.
MsgBox("Unexpected response: " & xHttp.Status & " - " & xHttp.StatusText, 48, "Error")
End If
因为您正在尝试从 SharePoint 网站下载,所以请求失败并返回HTTP 403 Forbidden
不提供任何身份验证的可能性很大。
推荐阅读
- php - Yii2 Basic-如何使用模型在循环中显示数据表?
- java - JPA HBM2DDL 创建无效的外键约束
- swiftui - NumberField 或如何使 TextField 输入 Double、Float 或其他带点的数字
- php - 使用 PHP GD 库将 webp 转换为 jpeg
- javascript - Function .__ proto__ 为什么不是 Function 的实例
- java - Java流:对象属性更新困难
- ios - SwiftUI 中月份的开始和结束日期
- node.js - JSDOM - nodejs 干净退出,不加载
- python-3.x - NoneType 对象不可调用
- yii2 - Yii2 ListView 不显示 HTML