Files with arabic name won't upload

ALI ALHOWSWE 191 Reputation points
2021-10-11T03:41:21.907+00:00

Hello guys
I have xamarin form app where users can upload file normally and it works fine until i found this problem if the image name written in arabica ex:"١٢٣٤٥٦.jpg" the image wont the upload fail . i am using this plugin CrossMedia

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
{count} votes

Accepted answer
  1. ALI ALHOWSWE 191 Reputation points
    2021-10-18T00:43:21.287+00:00

    I found the solution for my problem :
    i read the stream instead of the file path

            byte[] b = ReadToEnd(stream);  // olde code = File.ReadAllBytes(SelectedImageFile.Path.ToString());
    

    this is the function to convert the stream to baits :

    public static byte[] ReadToEnd(System.IO.Stream stream)
            {
                long originalPosition = 0;
    
                if (stream.CanSeek)
                {
                    originalPosition = stream.Position;
                    stream.Position = 0;
                }
    
                try
                {
                    byte[] readBuffer = new byte[4096];
    
                    int totalBytesRead = 0;
                    int bytesRead;
    
                    while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                    {
                        totalBytesRead += bytesRead;
    
                        if (totalBytesRead == readBuffer.Length)
                        {
                            int nextByte = stream.ReadByte();
                            if (nextByte != -1)
                            {
                                byte[] temp = new byte[readBuffer.Length * 2];
                                Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                                Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                                readBuffer = temp;
                                totalBytesRead++;
                            }
                        }
                    }
    
                    byte[] buffer = readBuffer;
                    if (readBuffer.Length != totalBytesRead)
                    {
                        buffer = new byte[totalBytesRead];
                        Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                    }
                    return buffer;
                }
                finally
                {
                    if (stream.CanSeek)
                    {
                        stream.Position = originalPosition;
                    }
                }
            }
    

    Thanks to anyone tried to help i hope this help someone

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. ALI ALHOWSWE 191 Reputation points
    2021-10-14T09:04:34.173+00:00

    Hi thanks @Leon Lu (Shanghai Wicresoft Co,.Ltd.)
    This my code :

     await CrossMedia.Current.Initialize();  
                if (!CrossMedia.Current.IsPickPhotoSupported)  
                {  
                    await DisplayAlert("", "نوع الصورة غير مدعوم", "موافق");  
                    return;  
                }  
                var MediaOptino = new PickMediaOptions()  
                {  
                    PhotoSize = PhotoSize.Medium  
                };  
      
                //if (Device.RuntimePlatform.Equals("iOS")) { await Task.Delay(100); }  
                var SelectedImageFile = await CrossMedia.Current.PickPhotoAsync(MediaOptino);  
                if (SelectedImageFile == null)  
                {  
                    await DisplayAlert("", "لم تقم بإختيار صورة ", "موافق");  
                    return;  
                }  
      
      
                // ProfileImg.Source = ImageSource.FromStream(() => SelectedImageFile.GetStream());  
                /////////////Posting the file to server//////////////////////////////////////////  
                await PopupNavigation.Instance.PushAsync(new LoodingPopup());  
                var url = App.MainUrl + "/MyWebServices/LinkForUplode";  
                MultipartFormDataContent formData = new MultipartFormDataContent();  
      
                var t = SelectedImageFile.Path.ToString();  
      
                byte[] b =  File.ReadAllBytes(SelectedImageFile.Path.ToString());  
                var imageContent = new ByteArrayContent(b, 0, b.Length);  
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");  
                //SENT  
                formData.Add(imageContent, "file", SelectedImageFile.Path.ToString());  
                formData.Add(new StringContent(MY_POST_ID), "POST_ID");  
                //  
                var httpClient = new System.Net.Http.HttpClient();  
                var responseMsg = await httpClient.PostAsync(url, formData);  
                var MesResponse = await responseMsg.Content.ReadAsStringAsync();  
                 
                await Alert.MySucsselAlert("تم حفظ الصورة", Navigation);  
                await HJ_P.FetchpOSTIMGS(MY_POST_ID, Navigation);  
                await PopupNavigation.Instance.PopAsync();  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.