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

Import http multipart content into a table

$
0
0

Hi, I am using the below code to import http content into a SQL Server table. The file format looks fine and when I test the import either from ARC or Postman this works fine. But, on the source system when I use this http endpoint looks like the http content is of multipart format. How to import multipart format into a SQL Server table.

public class DataController : ImportController
    {
        private IList<string> errors = new List<string>();

        [HttpPost]
        [Route("data/import", Name = "DataImport")]
        public IHttpActionResult Post()
        {
            var content = parse(this.Request.Content);

            postdata(content.lines);
            if (errors.Count == 0)
                return Ok("Inserted");
            else
                return Ok(errors);
        }

        private Models.DataImportFormat parse(HttpContent content)
        {
            var stream = content.ReadAsStreamAsync().Result;

            var parser = new MultipartParser(stream);

            if (parser.Success)
            {
                var newDataImportFormat = new Models.DataImportFormat
                {
                    fileData = parser.FileContents,
                    contentType = parser.ContentType,
                    fileName = parser.Filename,
                    lines = Encoding.UTF8.GetString(parser.FileContents).Split(new string[] { Environment.NewLine, @"\r" }, StringSplitOptions.None).ToList()
                };

                return newDataImportFormat;
            }

            return new Models.DataImportFormat();
        }

       private void importdata(IList<string> lines)

        {

            string connectionString = @"Data Source=localhost;Initial Catalog=TEST_DB;Integrated Security=True";



            var Data = from line in lines

                        let data = line.Split(',')

                        select new filedata

                        {

                            ID = data[0],
                            Type = data[1],
                            Status = data[2],
                            Description = data[3]

                        };



            using (SqlConnection sqldbConnection = new SqlConnection(connectionString))

            {

                        sqldbConnection.Open();

                            foreach (var i in Data)

                            {

                                try

                                {

                                    using (SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[testImport] ([ID], [Type], [Status], [Description]) VALUES (@ID, @Type, @Status, @Description)", sqldbConnection))

                                    {

                                        cmd.Parameters.AddWithValue("@ID", i.ID);
                                        cmd.Parameters.AddWithValue("@Type", i.Type);
                                        cmd.Parameters.AddWithValue("@Status", i.Status);
                                        cmd.Parameters.AddWithValue("@Description", i.Description);

                                        cmd.ExecuteNonQuery();

                                    }

                                }

                                catch (Exception ex)

                                {

                                    Console.WriteLine(ex.Message);

                                }



                            }

            }

        }


    }




Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

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