尝试使用 vb.net 将文件上传到 sinatra
我正在尝试使用 VB.Net 将文件上传到 Sinatra Web 服务,但我不确定如何配置任一端。当我运行 VB.Net 应用程序时,sinatra 总是以代码 404 响应。这是 VB.Net 代码,我从另一篇 SO 帖子转换而来:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim responseData As String = ""
Dim rdr As FileStream = New FileStream("X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy", FileMode.Open)
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://finqueryserver:9898"), HttpWebRequest)
req.Method = "POST" ' you might use "POST"
req.ContentLength = rdr.Length
req.AllowWriteStreamBuffering = True
Dim reqStream = DirectCast(req.GetRequestStream(), Stream)
Dim inData(rdr.Length) As Byte
' Get data from upload file to inData
Dim bytesRead As Integer = rdr.Read(inData, 0, rdr.Length)
' put data into request stream
reqStream.Write(inData, 0, rdr.Length)
rdr.Close()
Try
req.GetResponse()
Catch ex As Exception
responseData = "An error occurred: " & ex.Message
End Try
' after uploading close stream
reqStream.Close()
End Sub
这是 Sinatra 代码:(
require 'rubygems'
require 'sinatra'
post '/:name/:filename' do
puts "got here"
begin
name = params[:name]
rescue
name = "no name"
end
begin
filename = params[:filename]
rescue
filename = "no filename"
end
end
我从未见过“到达这里”。)Sinatra代码基于使用 cURL 的教程,但我不是。我也尝试
post "/:filename'
过给出 404 ,并且简单地
post "/"
显示“到达这里”,但显然没有用,因为我需要处理该文件。
显然我在这两个方面都是新手,这并不难,但我不知道该怎么做。
谢谢。
I am trying to use VB.Net to upload a file to a Sinatra web service, and I'm not sure how to configure either end. When I run the VB.Net app, sinatra invariably responds with code 404. Here's the VB.Net code, which I converted from another SO post:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim responseData As String = ""
Dim rdr As FileStream = New FileStream("X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy", FileMode.Open)
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://finqueryserver:9898"), HttpWebRequest)
req.Method = "POST" ' you might use "POST"
req.ContentLength = rdr.Length
req.AllowWriteStreamBuffering = True
Dim reqStream = DirectCast(req.GetRequestStream(), Stream)
Dim inData(rdr.Length) As Byte
' Get data from upload file to inData
Dim bytesRead As Integer = rdr.Read(inData, 0, rdr.Length)
' put data into request stream
reqStream.Write(inData, 0, rdr.Length)
rdr.Close()
Try
req.GetResponse()
Catch ex As Exception
responseData = "An error occurred: " & ex.Message
End Try
' after uploading close stream
reqStream.Close()
End Sub
And here is the Sinatra code:
require 'rubygems'
require 'sinatra'
post '/:name/:filename' do
puts "got here"
begin
name = params[:name]
rescue
name = "no name"
end
begin
filename = params[:filename]
rescue
filename = "no filename"
end
end
(I never see "got here".)The Sinatra code is based on tutorials using cURL, which I am not. I've also tried
post "/:filename'
which gives 404 also, and simply
post "/"
which does show "got here", but is obviously useless because I need to deal with the file.
Clearly I'm a newbie at both, this can't be that hard, but I'm not sure what to do.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,为了后代,这就是我为使其发挥作用所做的事情。在 vb.net 代码中,我使用:
在 sinatra 文件中:
Ok, well for posterity, here's what I did to get it to work. In the vb.net code, I used:
and in the sinatra file: