2011-02-28 88 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