我收到一个我无法弄清楚的错误代码!在我的vba中,我正在创建一个excel文件并使用POST将其上传到服务器。我可以手动导入这个文件没问题,但由于某种原因,我的代码从服务器得到失败响应 - 代码415,损坏的表单数据:过早结束。
Sub offline_punch()
Dim newwkb As Workbook
Dim thiswkb As Workbook
Dim URLReportop As String
Dim myrequestop As Object
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set thiswkb = ActiveWorkbook
Set StartCell = thiswkb.Worksheets("offline_punch").Range("A1")
Set newwkb = Workbooks.Add
'Find LastRow and LastColumn
LastRow = thiswkb.Worksheets("offline_punch").Cells(thiswkb.Worksheets("offline_punch").Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = thiswkb.Worksheets("offline_punch").Cells(StartCell.Row, thiswkb.Worksheets("offline_punch").Columns.Count).End(xlToLeft).Column
thiswkb.Worksheets("offline_punch").Range(StartCell, thiswkb.Worksheets("offline_punch").Cells(LastRow, LastColumn)).Copy
newwkb.Worksheets("sheet1").Range("A1").PasteSpecial xlPasteValues
newwkb.SaveAs ("E:\Documents\PunchImport" & Format(Now(), "yyyymmddhhmmss") & ".xls"), 56
thiswkb.Activate
Call getToken
'Send raw punch import to wfr
Set myrequestop = CreateObject("winhttp.winhttprequest.5.1")
URLReportop = ("https://secure.saashr.com:443/ta/rest/v1/import/119")
myrequestop.Open "POST", URLReportop, False
myrequestop.setRequestHeader "Authentication", "Bearer " & Token
myrequestop.setRequestHeader "Content-Type", "multipart/form-data; boundary=abcdefghi-jklmnop; charset=UTF-8"
myrequestop.setRequestHeader "Content-length", 1000
myrequestop.Send newwkb
MsgBox (myrequestop.responseText & " " & Now)
newwkb.Close
Set newwkb = Nothing
thiswkb.Activate
End Sub
答案 0 :(得分:0)
编辑 - 添加使用正确文件长度的代码
在子添加的顶部:
Dim strFilename As String
Dim lngFileLen as Long
将您的通话更改为newwkb.SaveAs
至这三行
strFilename = "E:\Documents\PunchImport" & Format(Now(), "yyyymmddhhmmss") & ".xls"
newwkb.SaveAs (strFilename), 56
lngFileLen = FileLen(strFilename)
更改此行
myrequestop.setRequestHeader "Content-length", 1000
到此
myrequestop.setRequestHeader "Content-length", lngFileLen