Quantcast
Channel: C# Save multiple file in on server and gather their paths in in one cell , are this possible ?
Viewing all articles
Browse latest Browse all 3

C# Save multiple file in on server and gather their paths in in one cell , are this possible ?

$
0
0

here is a script to upload multiple file on server folder and store their path in database, however the path of the images are getting stored in multi Row.

For example: i upload 3 images, image1.jpg, image2.jpg, image3.jpg. These images are supposed to get stored in 1 Row

Now problem is that the path of all 3 images are getting stored under 3 row. the path

uploads/image1.jpg in row  1 
uploads/image1.jpgimage2.jpg  row2
uploads/image1.jpgimage2.jpgimage3.jpg in row3

 

i wish that images should get stored in this way:

uploads/image1.jpg*uploads/image1.jpgimage2.jpg*uploads/image1.jpgimage2.jpgimage3.jpg

1-   Multiple File Upload Using JQuery Multifile 

This script :

https://github.com/BoSchatzberg/jfu-CSharp-Example/blob/master/README

and this is

html form

<formid="file_upload"runat="server">

       <divid="filediv">

           <inputtype="file"name="file"multiple>

           <button>Upload</button>

           <div>Upload files</div>

       </div>

       <tableid="files"></table>

2-   I modified FileUpload.ashx to save all file paths in dataset to convert it to xml file and finally save it in one cell

FileUpload.ashx.cs

 publicvoid ProcessRequest(HttpContext context)

        {

 

           if (context.Request.Files.Count == 0)

            {

 

               //LogRequest("No files sent.");

 

                context.Response.ContentType ="text/plain";

                context.Response.Write("No files received.");

 

            }

           else

            {

               // create table to save all file paths in on file 

               DataTable DT =newDataTable("FileTable");

               DataColumn dc =newDataColumn("FilePath",typeof(string));

                DT.Columns.Add(dc);

 

               HttpFileCollection hfc =HttpContext.Current.Request.Files;

 

               for (int i = 0; i < hfc.Count; i++)

                {

                   HttpPostedFile hpf = hfc[i];

                   if (hpf.ContentLength > 0)

                    {

                       DataRow dr = DT.NewRow();

                        System.IO.Path.GetFileName(hpf.FileName);

                        dr["FilePath"] = "upload"+ hpf.FileName;

                        DT.Rows.Add(dr);

                        hpf.SaveAs(HttpContext.Current.Server.MapPath("/Upload")+ "\\"+ hpf.FileName);

                       string FileName = hpf.FileName;

                       string FileType = hpf.ContentType;

                       int FileSize = hpf.ContentLength;

 

                context.Response.ContentType ="text/plain";

                context.Response.Write("{\"name\":\""+ FileName + "\",\"type\":\""+ FileType +"\",\"size\":\""+ FileSize +"\"}");

                    }

                }

               DataSet ds =newDataSet();

                ds.Tables.Add(DT);

               // connect to data base to pass Procedure  ds.GetXml()

               SqlConnection conn =newSqlConnection(ConfigurationManager.ConnectionStrings["sundbc"].ToString());

            

               var cmd =newSqlCommand("sp_SaveFile", conn);

                cmd.CommandType =CommandType.StoredProcedure;

                cmd.Connection = conn;

                conn.Open();

                cmd.Parameters.AddWithValue("@xmlFile", ds.GetXml());

                 cmd.ExecuteNonQuery();

                                 

               // Data is accessible through the DataReader object here.

 

                conn.Close();

 

               

            }

 

        }

 


Viewing all articles
Browse latest Browse all 3

Trending Articles



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