gpt4 book ai didi

php - 通过http上传word文档

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:23 26 4
gpt4 key购买 nike

我认为如果我可以使用纯 VBA 从 Word 中上传 word 文档到我的网站会非常方便,没有插件等等......

作为起点,我想在我的根目录 c:\reg.txt 中上传一个文件,一旦我开始工作,我希望我的宏将当前文档保存在某个地方,然后上传它。

到目前为止,我的 VBA 代码是这样的:

Sub UploadFile()
'
' UploadFile Macro
'
'
strURL = "http://www.mywebsite.com/files/upload.php"
File = "c:\reg.txt"

Set HTTP = CreateObject("Microsoft.XMLHTTP")

Set objStream = CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile (File)

HTTP.Open "POST", strURL, False
MsgBox "Now uploading file " & File

HTTP.setRequestHeader "Content-Type", "multipart/form-data;"
HTTP.send objStream.Read
MsgBox (HTTP.responseText)
MsgBox "Uploading complete for file " & File

End Sub

我的 php 脚本是这样的:

<?php

$uploaddir = '/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo $uploadfile;
echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

我意识到我的客户端和服务器端代码都可能存在多个错误,因为我正在尝试调整我发现的各种示例,但我将非常感谢任何指示!

最佳答案

好消息 - 我最终解决了这个问题,我希望这段代码对其他人有用!最后我选择用 asp 编写我的服务器端代码,因为我真的不知道我在用 php 做什么!

Sub UploadFile()
'
' UploadFile Macro
'
'
Dim objStream, objHttp
Dim sUrl, sFile, sName

ActiveDocument.Save
FileName = ActiveDocument.Name
Application.Documents.Add FileName
ActiveDocument.SaveAs "c:\temp\" & FileName
ActiveDocument.Close

sFile = "c:\temp\" & ActiveDocument.Name
UUID = "xxxx"
FUID = "yyyy"
sName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))

sUrl = "http://www.mywebsite.com/test.asp?File=" & sName & "&User=Ed&UUID=" & UUID & "&FUID=" & FUID

Set objStream = CreateObject("ADODB.Stream")
objStream.Mode = 3
objStream.Type = 1
objStream.Open
objStream.LoadFromFile (sFile)

If Err = 0 Then
MsgBox "Uploading ... please wait"
End If

Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")
objHttp.Open "POST", sUrl, False
objHttp.SetRequestHeader "Content-Length", objStream.Size
objHttp.Send objStream.Read(objStream.Size)

If Err = 0 Then
MsgBox objHttp.responseText
Else
MsgBox "Upload Error!" & vbCrLf & Err.Description
End If

objStream.Close
Set objStream = Nothing
Set objHttp = Nothing

End Sub

关于php - 通过http上传word文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29542782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com