FosterZ
FosterZ

Reputation: 3911

File upload in asp.net c#

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 ?

Upvotes: 0

Views: 3952

Answers (1)

Furqan Hameedi
Furqan Hameedi

Reputation: 4400

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.

Upvotes: 1

Related Questions