帖子文件服务器asp.net使用XMLHTTP [英] Post file to Server asp.net using XMLHttp

查看:156
本文介绍了帖子文件服务器asp.net使用XMLHTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用宏启用Word文件。我需要保存给予的URL和一个aspx页面张贴到使用VBA code这里的服务器上的文件是我做了什么。

I use an Macro enabled word file. I need to save the document on the server given an URL and an aspx page to post to using VBA code here is what i have done .

'sends multipart/form-data To the URL using WinHttprequest/XMLHTTP
'FormData - binary (VT_UI1 | VT_ARRAY) multipart form data
Function WinHTTPPostRequest(URL, FormData, Boundary)
  'Dim http As New MSXML2.XMLHTTP

  'Create XMLHTTP/ServerXMLHTTP/WinHttprequest object
  'You can use any of these three objects.
  'Set http = CreateObject("WinHttp.WinHttprequest.5")
  Set http = CreateObject("MSXML2.XMLHTTP")
  'Set http = CreateObject("MSXML2.ServerXMLHTTP")

  'Open URL As POST request
  http.Open "POST", URL, False

  'Set Content-Type header
  'http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
  http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  'Send the form data To URL As POST binary request
  http.send (FormData)

  'Get a result of the script which has received upload
  MsgBox http.responseText
  WinHTTPPostRequest = http.responseText
End Function

// --------------------------------------------- ----

//-------------------------------------------------

Sub UploadFile(DestURL As String, FileName As String, Optional ByVal FieldName As String = "File")

  DestURL = "http://192.168.1.41/s11/Journal.aspx?guid=fb492030-978f-4105-92f6-28a64959c612"
  Dim sFormData As String, d As String

  'Boundary of fields.
  'Be sure this string is Not In the source file
  Const Boundary As String = "---------------------------0123456789012sdsdssdsdsd"

  'Get source file As a string.
  sFormData = GetFile(FileName)

  'Build source form with file contents
  d = "--" + Boundary + vbCrLf
  d = d + "Content-Disposition: form-data; name=""" + FieldName + """;"
  d = d + " filename=""" + FileName + """" + vbCrLf
  d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
  d = d + sFormData
  d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf

  'Post the data To the destination URL
  WinHTTPPostRequest DestURL, sFormData, Boundary
End Sub

// --------------------------------------------- -----------

//--------------------------------------------------------

'read binary file As a string value
Function GetFile(FileName As String) As String
  Dim FileContents() As Byte, FileNumber As Integer
  ReDim FileContents(FileLen(FileName) - 1)
  FileNumber = FreeFile
  Open FileName For Binary As FileNumber
    Get FileNumber, , FileContents
  Close FileNumber
  GetFile = StrConv(FileContents, vbUnicode)
End Function

// --------------------------------------------- -----------

//--------------------------------------------------------

Private Sub Document_Close()
UploadFile "", "E://Rd.docx"
End Sub

// --------------------------------------------- -----------

//--------------------------------------------------------

它发布到网页,但我不能够访问该文件?我必须失去了一些东西?
请发现与样品code 示例XML HTTP文档保存<附文件/ A>

It posts to the page but i am not able to access the file ? I must be missing something ?? Please find attached document with the sample code Sample XML HTTP DOCUMENT SAVE

推荐答案

我意识到现在这个是一老一少,除了你有没有真正的前$ P $的真正问题的形式pssed这个(不被声明后挑剔的,但添加问号不会自动使他们的问题!)。

I realise this is a little old now and in addition you haven't actually expressed this in the form of a genuine question (not to be pernickety, but adding question marks after statements do not automatically make them questions!).

但答案似乎很简单:即使你做UploadFile各个步骤设置变量d正确的信息提交,你那去,并通过sFormData(是的GetFile而造成的)

However the answer seems fairly simple: even though you do various steps in UploadFile to set up the variable d with the right information to submit, you then go and pass sFormData (which is just the result of GetFile)

要纠正它,你应该调用WinHTTPPostRequest与前更换行:

To rectify it, you should replace the line before the call to WinHTTPPostRequest with:

 sFormData = d + vbCrLf + "--" + Boundary + "--" + vbCrLf

这篇关于帖子文件服务器asp.net使用XMLHTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆