Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to upload Image using Web API

$
0
0

Hi All,

I have file upload code is as follows. What will be Web API code for save the file and how I can post the image data to Web API

<form novalidate name="f1" ng-submit="SaveFile()"><div style="color: red">{{Message}}</div><table><tr><td>Select File : </td><td><input type="file" name="file" accept="image/*" onchange="angular.element(this).scope().selectFileforUpload(this.files)" required /><span class="error" ng-show="(f1.file.$dirty || IsFormSubmitted) && f1.file.$error.required">Image required!</span><span class="error">{{FileInvalidMessage}}</span></td></tr><tr><td>Description : </td><td><input type="text" name="uFileDescription" ng-model="FileDescription" class="{{(IsFormSubmitted?'ng-dirty' + (f1.uFileDescription.$invalid?' ng-invalid':''):'')}}" autofocus /></td></tr><tr><td></td><td><input type="submit" value="Upload File" /></td></tr></table></form>

I have action method code for save the image file but Need to convert into web API method

 [HttpPost]
        public string SaveFiles(string description)
        {
            string Message = "";

            string fileName, actualFileName;
            Message = fileName = actualFileName = string.Empty;
            bool flag = true;
            var allDatawithmsg = new Data();
            if (Request.Files != null)
            {
                var file = Request.Files[0];
                actualFileName = file.FileName;

                fileName = file.FileName;
                int size = file.ContentLength;
                var folderpath = @"C:/Images/MoreInfo/";
                foreach (string fileinfo in Directory.GetFiles(folderpath))
                {
                    string FileName = Path.GetFileName(fileinfo);
                    if (actualFileName == FileName)
                    {
                        flag = false;
                    }

                }

                if (flag)
                {
                    try
                    {

                        file.SaveAs(Path.Combine(folderpath, fileName));
                        byte[] photo = GetPhoto(Path.Combine(folderpath, fileName));
                        context.sp_updateImageData(actualFileName, fileName, description, size, photo);
                        allDatawithmsg.successMessage = "File upload successfully";
                        allDatawithmsg.imageTableData = GetImageInfo();
                    }
                    catch (Exception)
                    {
                        allDatawithmsg.unSuccessMessage = "File upload failed! Please try again";
                    }
                }
                else
                {
                    allDatawithmsg.unSuccessMessage = "File already exists ! Please try again";
                }

            }
            var setting = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            return JsonConvert.SerializeObject(allDatawithmsg, Formatting.Indented, setting);

        }


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>