post imagefile to upload server with api

shaghayeghshaghayegh USMember ✭✭✭

hi

`

var client = new RestClient("http://api.parsaspace.com/v1/files/upload");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization","Bearer yourtoken");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("domain","yourdomain");
request.AddParameter("path","/");
request.AddFile("file",yourfile);
IRestResponse response = client.Execute(request);

`
i test it in postman. it worked.
but when i use it in xamarin(android) it doesnt work.
i have this problem that what is "yourfile"?
i put for example "C:\index.jpg" but it gave error that file not found.
what should i use in string path "yourfile'?

Answers

  • PaulNTUPaulNTU USMember ✭✭✭

    The "yourfile" is the full path to your file. The problem you will have is that the file system on Android (and phones in general) is very different to windows. There is no C: drive for example and your application is restricted to its own working space. You can access external folders but you are limited to which ones you can access.

    You should probably look at the Xamarin Essentials filesystem helper to give you assistance in handling and accessing files on a mobile device
    https://docs.microsoft.com/en-us/xamarin/essentials/file-system-helpers?context=xamarin/xamarin-forms&tabs=android

    Paul

  • shaghayeghshaghayegh USMember ✭✭✭

    i tested it on mobile but it didnt worked.

  • shaghayeghshaghayegh USMember ✭✭✭

    get image by intent code

  • shaghayeghshaghayegh USMember ✭✭✭

    i use this code for get image path

    ` btnimg.Click += delegate {
    GetImage(((b, p) => {
    Toast.MakeText(this, "Found path: " + p, ToastLength.Long).Show();
    filepath = p;
    Toast.MakeText(this, filepath , ToastLength.Long).Show();
    }));

            }; public delegate void OnImageResultHandler(bool success, string imagePath);
    
        protected OnImageResultHandler _imagePickerCallback;
        public void GetImage(OnImageResultHandler callback)
        {
            if (callback == null)
            {
                throw new ArgumentException("OnImageResultHandler callback cannot be null.");
            }
    
            _imagePickerCallback = callback;
            InitializeMediaPicker();
        }
    
        public void InitializeMediaPicker()
        {
            Intent = new Intent();
            Intent.SetType("image/*");
            Intent.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), 1000);
        }
    
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if ((requestCode != 1000) || (resultCode != Result.Ok) || (data == null))
            {
                return;
            }
    
            string imagePath = null;
            var uri = data.Data;
            try
            {
                imagePath = GetPathToImage(uri);
            }
            catch (Exception ex)
            {
                // Failed for some reason.
            }
    
            _imagePickerCallback(imagePath != null, imagePath);
        }
    
        private string GetPathToImage(Android.Net.Uri uri)
        {
            ICursor cursor = this.ContentResolver.Query(uri, null, null, null, null);
            cursor.MoveToFirst();
            string document_id = cursor.GetString(0);
            document_id = document_id.Split(':')[1];
            cursor.Close();
    
            cursor = ContentResolver.Query(
            Android.Provider.MediaStore.Images.Media.ExternalContentUri,
            null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new String[] { document_id }, null);
            cursor.MoveToFirst();
            string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
            cursor.Close();
    
            return path;
        }
    

    `

Sign In or Register to comment.