Problem sending image to database

Asked

Viewed 39 times

0

I have a web application in which the user can change his profile image, I am able to select the image file correctly, the problem is time to send to the database (send the application data to a webservice and websService to the database), which returns me error 400.

HTML:

<div id="userImage">
   <image id="profileImage" src="" />
   <input id="imageUpload" type="file" name="profile_photo" placeholder="Photo" required="" capture>
</div>

Javascript (Sending to webservice):

function UpdateImage(email){
    var img1 = document.getElementById('profileImage').src;
    var body = JSON.stringify({ "UserName": email, "UserImage": img1 });
    console.log(img1);
    $.ajax({
        url: 'minha rota',
        headers: {
            'Content-Type': 'application/json'
        },
        type: 'Post',
        crossDomain: true,
        cache: false,
        processData: false,
        data: body,
        success: function (result) {
            console.log(result);
        }
    });
}

Web service method filling the database (C#):

[HttpPost]
        [Route("minha rota")]
        public HttpResponseMessage PostImage(IdUser user)
        {  
            try
            {
                byte[] images = null;
                FileStream Streem = new FileStream(user.UserImage, FileMode.Open, FileAccess.Read);
                BinaryReader brs = new BinaryReader(Streem);
                images = brs.ReadBytes((int)Streem.Length);

                bool result = false;

                using (SqlConnection connection = new SqlConnection(this.ConnectionString))
                {
                    connection.Open();

                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        command.CommandText = "insert into IdUser (UserImage) values (@images) where UserName = @username";

                        command.Parameters.AddWithValue("images", images);
                        command.Parameters.AddWithValue("username", user.UserName);

                        int i = command.ExecuteNonQuery();
                        result = i > 0;
                    }

                    connection.Close();
                }

                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }

        }

Error occurs on line FileStream Streem = new FileStream(user.UserImage, FileMode.Open, FileAccess.Read); returning error 400 :

The specified path, file name or both are very long. The fully qualified file name must be less than 260 characters and directory name less than 248 characters.

The value that is arriving in the webservice is a route of type Base64 with size of 7000 characters, which is obviously the cause of the error. The problem is that apparently a Base64 route cannot be reduced. I have spent a few days researching, I would like to know how to solve this problem.

  • the first parameter there is a path to the ex file : "c: test.jpg", you are trying to put a Base64, so the error

  • @Lucas Miranda What parameter are you referring to ? " Filemode.Open" ?

  • this user.Userimage, I imagine that’s where your Base64

  • @Lucas Miranda Correct, but this field (Userimage) is a string.

  • yes, but it is to be a string containing the file path, not a Base64, its error occurs because in general a path does not have the huge amount of characters that a Base64 has

  • @Lucas Miranda Is there any way to convert Base64 to a "c: test.jpg route" ?

Show 1 more comment

1 answer

1


Note that you are trying to convert your Base64 into a stream to then convert it to bytes

byte[] images = null;
FileStream Streem = new FileStream(user.UserImage, FileMode.Open, FileAccess.Read);
BinaryReader brs = new BinaryReader(Streem);
images = brs.ReadBytes((int)Streem.Length);

I believe that you do not need to do this because it is possible to transform a Base64 directly into bytes using the Convert, would look like this:

byte[] images = null;                     
images = System.Convert.FromBase64String(user.UserImage);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.