0

hey guys, m using an api of "Bits on the Run" following is the code of upload API

   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 "";
        }   
    }

and below is my code to upload a file, m using FileUpload control to get the full path of a file(but m not succeeded in that)...

            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);

i read in some forums saying that for some security purpose you can't get fullpath of a file from client side. If it is true then how can i achieve file upload in my scenario ?

1
  • what is the .NET version? is plain old ASP.NET or ASP.NET MVC? please add all in the tags Commented Feb 28, 2011 at 12:02

1 Answer 1

1

Yes, this is true, for security reason you cannot get the fullpath of the client machine, what you can do is, try the following,

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

instead of creating your own FileStream use the stream provided by the FileUploadControl. Hoep it shall help.

Sign up to request clarification or add additional context in comments.

4 Comments

i can use Stream instead of FileStream but the API method Upload excepts a filepath as its 3rd parameter, how can i give a full file path to it
Once you have the "bytes" byte[], you can save it in some temporary location on your webserver and provide that path to your control.
@Furqn:thanx dude, it worked, but is it possible to store that byte[] in windows temp folder n delete later ?
Yup, its possible but not recommended, you should do it in some application specific temp folder, since you have to give permissions to asp.net user or any other user you impersonate for saving file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.