将文件路径转换为URI

时间:2012-05-13 12:04:10

标签: ruby

我有一个表示文件路径的字符串,例如“/home/user/dir1/dir2/dir3/name.ext”。现在我想将它转换为合法的URI(以“file://”开头的字符串等)。

我如何在Ruby中做到这一点?

2 个答案:

答案 0 :(得分:6)

require 'uri'

uri = URI.join('file:///', '/home/user/dir1/dir2/dir3/name.ext')
 => #<URI::Generic:0x0000000263fcc0 URL:file:/home/user/dir1/dir2/dir3/name.ext>

uri.scheme
 => "file"
uri.path
 => "/home/user/dir1/dir2/dir3/name.ext"
uri.to_s
 => "file:/home/user/dir1/dir2/dir3/name.ext"

答案 1 :(得分:0)

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
    req.write('hello');
  }
  else {
    console.log("Error "+response.statusCode)
    req.write('hellos');
  }
})