2011-02-28 103 views
0

嗨,男使用“Bits on the Run”下面的API是上传AP​​I的代码文件上传

public string Upload(string uploadUrl, NameValueCollection args, string filePath) 
    { 
     _queryString = args; //no required args 

     WebClient client = createWebClient(); 
     _queryString["api_format"] = APIFormat ?? "xml"; //xml if not specified - normally set in required args routine         
     queryStringToArgs(); 

     string callUrl = _apiURL + uploadUrl + "?" + _args; 
     callUrl = uploadUrl + "?" + _args; 

     try { 
      byte[] response = client.UploadFile(callUrl, filePath); 
      return Encoding.UTF8.GetString(response);  
     } catch { 
      return ""; 
     } 
    } 

以下是我的代码上传文件,男使用FileUpload控件获取文件的完整路径(但在m未成功)......

  botr = new BotR.API.BotRAPI("key", "secret_code"); 
      var response = doc.Descendants("link").FirstOrDefault(); 
      string url = string.Format("{0}://{1}{2}", response.Element("protocol").Value, response.Element("address").Value, response.Element("path").Value); 
      //here i want fullpath of the file, how can i achieve that here 
      string filePath = fileUpload.PostedFile.FileName;//"C://Documents and Settings//rkrishna//My Documents//Visual Studio 2008//Projects//BitsOnTheRun//BitsOnTheRun//rough_test.mp4"; 

      col = new NameValueCollection(); 

      FileStream fs = new FileStream(filePath, FileMode.Open); 

      col["file_size"] = fs.Length.ToString(); 
      col["file_md5"] = BitConverter.ToString(HashAlgorithm.Create("MD5").ComputeHash(fs)).Replace("-", "").ToLower(); 
      col["key"] = response.Element("query").Element("key").Value; 
      col["token"] = response.Element("query").Element("token").Value; 

      fs.Dispose(); 
      string uploadResponse = botr.Upload(url, col, filePath); 

我在一些论坛读说,对于一些安全目的,你不能从客户端文件的FULLPATH。如果它是真的,那我怎么能在我的场景中实现文件上传?

+0

什么是.NET版本?是普通的旧的ASP.NET或ASP.NET MVC?请在标签中添加全部 – balexandre 2011-02-28 12:02:24

+0

其原始的ASP.NET 3.5 – FosterZ 2011-02-28 12:03:25

回答

1

是的,这是真实的,出于安全原因,你不能得到客户端机器的FULLPATH,你可以做的是,尽量的

 Stream stream = fileUpload.PostedFile.InputStream; 
     stream.Read(bytes, 0, fileUpload.PostedFile.ContentLength); 

,而不是创建自己的FileStream使用流提供了以下,通过FileUploadControl。它会帮助。

+0

我可以使用'Stream'而不是'FileStream',但是API方法'Upload'除了文件路径作为第三个参数,我怎么能给它一个完整的文件路径 – FosterZ 2011-02-28 12:15:59

+0

一旦获得了“字节”字节[],就可以将其保存在Web服务器上的某个临时位置,并将该路径提供给您的控件。 – 2011-02-28 12:30:37

+0

@Furqn:thanx老兄,它的工作,但它可能存储该字节[]在Windows临时文件夹n删除以后? – FosterZ 2011-03-01 10:36:46