Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
i want to be able to send the form into my sqldatabase and save the image in a folder and the path in the database

What I have tried:

.directive("fileInput",function($parse){
                return {
                    restrict: "A",
                    link: function (scope,elem,attrs) {
                        elem.bind("change", function () {
                            $parse(attrs.fileInput).assign(scope, elem[0].files);
                            scope.$apply();
                        });
                    }
                }
            })
.controller("imageUploadPracticeController", function ($scope,$http,$timeout) {
                var vm = this;
                vm.loading = false;
                vm.msg = false;
                vm.error = false;
               // read();

                vm.imageUpload = function (event) {
                    var files = event.target.files;//filelist object
                    var file = files[files.length - 1];
                    vm.file = file;
                    var reader = new FileReader();
                    reader.onload = vm.imageIsLoaded;
                    reader.readAsDataURL(file);
                }
                vm.imageIsLoaded = function (e) {
                    $scope.$apply(function () {
                        vm.step = e.target.result;
                    });
                }

                vm.btnUpload = function () {
                    vm.loading = true;
                    var request = $http({
                        url: '../services/svImageUpload.asmx/insert',
                        method: 'POST',
                        data: { city: vm.City, country: vm.Country, flag: vm.file.name },
                        //transformRequest:angular.identity,
                        headers: {'content-type':undefined}
                    });
                    request.then(function (response) {
                        read();
                        vm.loading = false;
                        vm.error = false;
                        vm.hurray = response.data;
                        vm.msg = true;
                    }, function (error) {
                        vm.error = true;
                        vm.error = error.data;
                        vm.loading = false;
                        vm.msg = false;
                    });
                    //console.log(vm.City + ' ' + vm.Country + ' ' + vm.file.name);
                }
                var read = function () {
                    var request = $http("../services/svImageUpload.asmx/GetAllFiles")
                                  .then(function (response) { vm.imgUploads = response.data; });
                }
                vm.closeSuccessMsg = function () {
                    vm.loading = false;
                    vm.msg = false;
                    vm.error = false;
                    $("#txtCity").val('');
                    $("#txtCountry").val('');
                }
                vm.closeErrorMsg = function () {
                    vm.loading = false;
                    vm.msg = false;
                    vm.error = false;
                    $("#txtCity").val('');
                    $("#txtCountry").val('');
                }
            });


[WebMethod]
        public string insert(string city,string country,string flag)
        {
            try
            {
                using(SqlConnection conn =new SqlConnection(clsMain.sqlconnstring))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("spInsertUploadPicture", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@city", city);
                    cmd.Parameters.AddWithValue("@country",country);
                    cmd.Parameters.AddWithValue("@imgpath", string.Format("../imageUploads/{0}",flag));
                    cmd.ExecuteNonQuery();
                    return "Record Uploaded Successfully";
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
Posted
Updated 26-May-17 13:39pm
Comments
[no name] 26-May-17 13:33pm    
I want to be able to answer whatever your question is.
ZurdoDev 26-May-17 13:39pm    
Very easy to do. What is your question?

1 solution

On the client side, you have a load complete handler on your image (I assume that's the image you want in the database):

JavaScript
vm.imageIsLoaded = function (e) {
    $scope.$apply(function () {
        vm.step = e.target.result;
    });
}

You want to send that e.target.result to the server in your service call. Once there you parse off the beginning text that just identifies the media type. Then you have a perfectly good image as a base64 string. You can store it in the database as text, or continue on to convert to an Image and store it that way:

C#
string imageString = ThisValueIsETargetResultFromClient; // e.target.result;
imageString = imageString.Substring(imageString.IndexOf(",") + 1); // remove meta
// Now can save imageString to database as base 64 string, or

using(MemoryStream ms = new MemoryStream(Convert.FromBase64String(imageString)))
{
      System.Drawing.Image image = System.Drawing.Bitmap.FromStream(ms);
      // Now can save image to database as Image file
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900