0

The code below is used to upload images. I would like to know the Angular code for posting data and web API code to save images on server.

<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>

I have action method code for save the image file. Following code save the image file.

    [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);
    }

But I need to convert into web API method.How to achieve this?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.