尝试使用 vb.net 将文件上传到 sinatra

发布于 2024-12-03 09:28:45 字数 1715 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2024-12-10 09:28:45

好吧,为了后代,这就是我为使其发挥作用所做的事情。在 vb.net 代码中,我使用:

  qry_results = wc.UploadFile("http://192.168.9.81:9898/execfqy", "X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy")

在 sinatra 文件中:

 post '/execfqy' do
 qry_file.write(params[:file][:tempfile].readlines)

Ok, well for posterity, here's what I did to get it to work. In the vb.net code, I used:

  qry_results = wc.UploadFile("http://192.168.9.81:9898/execfqy", "X:\QueryTxtFiles\Query\CDA Curncy_9_1_2011.fqy")

and in the sinatra file:

 post '/execfqy' do
 qry_file.write(params[:file][:tempfile].readlines)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文