2

I have seen many examples with ng-flow having the php side server to upload the files. But as I am not an expert in php and I need some help in the webapi, can someone please help me to find a working example or tutorial of ng-flow with webapi files upload.

Thanks everyone.

rahulmr
  • 681
  • 1
  • 7
  • 19

1 Answers1

0

below is my web-api code to do this

[HttpPost]
        public async Task<HttpResponseMessage> SaveFile()
        {
            if (!Request.Content.IsMimeMultipartContent())
                Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);

            var provider = FileSaver.GetMultipartProvider();

            var result = await Request.Content.ReadAsMultipartAsync(provider);
            var fileInfo = FileSaver.MoveToTemp(result);
            return Request.CreateResponse(HttpStatusCode.OK, fileInfo);
        }

it uses the custom FileSaver class

public class FileSaver
{
    public static MultipartFormDataStreamProvider GetMultipartProvider()
    {
        var uploadFolder = //your upload path;
        return new MultipartFormDataStreamProvider(uploadFolder);
    }

    private static string GetDeserializedFileName(MultipartFileData fileData)
    {
        var fileName = GetFileName(fileData);
        return JsonConvert.DeserializeObject(fileName).ToString();
    }

    private static string GetFileName(MultipartFileData fileData)
    {
        return fileData.Headers.ContentDisposition.FileName;
    }

    public static FileInfo MoveToTemp(MultipartFormDataStreamProvider result)
    {
        var originalFileName = GetDeserializedFileName(result.FileData.First());
        var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);
        string timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
        var folder = Directory.CreateDirectory(**); //your upload path
        if (!folder.Exists) folder.Create();
        var filename = folder.FullName + @"\" + originalFileName;
        MoveFile(uploadedFileInfo, filename);
        return uploadedFileInfo;
    }

    private static void MoveFile(FileInfo fileInfo, string filename)
    {
        var count = 0;
        do
        {
            try
            {
                fileInfo.MoveTo(filename);
                return;
            }
            catch (Exception)
            {
                if (count == 4)
                {
                    throw;
                }
                count++;
                Thread.Sleep(1 * 1000);
            }
        } while (true);
    }
}
harishr
  • 17,807
  • 9
  • 78
  • 125
  • I am not able to access ColloSysParam. can you please tell me what exactly that is ? how do we configure the SaveFile() method ins the config of ng-flow ? – rahulmr Mar 16 '15 at 08:52
  • replace it by your upload path... edited the code accordingly.. you need to post the file contect using ng-flow... as per your question, you have problem on server side... – harishr Mar 16 '15 at 09:03