Upload image using WCF Rest WebService in ASP.Net

487vidya
 
on Dec 03, 2013 12:36 AM
5747 Views

How to upload and save images as (BLOB) in databse in asp.net using WCF

 

am getting images as bytes in my datatable..but returns error 400 if multiple images selected

 DocumentID = int.Parse(Request.QueryString["DocID"]);
                                byte[] fileData = new byte[file.InputStream.Length];

                                //Stream streamdata = new MemoryStream(fileData);

                                file.InputStream.Read(fileData, 0, Convert.ToInt32(file.InputStream.Length));
                                file = getfile.UploadedFiles[filecnt];
                                if (dtImage == null || dtImage.Rows.Count == 0)
                                {
                                    dtImage = new DataTable();
                                    dtImage.Columns.Add("REQID");
                                    dtImage.Columns.Add("DocType");
                                    dtImage.Columns.Add("FileType");
                                    dtImage.Columns.Add("uploadId");
                                    //dtImage.Columns.Add("DATA", typeof(Byte[]));
                                    //dtImage.Columns.Add("DATAstream", typeof(Stream));

                                    for (int i = 0; i <= 0; i++)
                                    {
                                        DataRow dtrowtar = dtImage.NewRow();
                                        dtrowtar[0] = DocumentID;
                                        dtrowtar[1] = ddlType.SelectedValue.ToString();
                                        dtrowtar[2] = file.GetExtension().ToString();
                                        dtrowtar[3] = 1;
                                        dtrowtar[4] = fileData;
                                        //dtrowtar[5] = streamdata;
                                        dtImage.Rows.Add(dtrowtar);
                                    }
                                }
                                else
                                {
                                    int DocIndex = dtImage.Rows.Count;
                                    dtImage.Rows.Add();
                                    dtImage.Rows[DocIndex]["REQID"] = dtImage.Rows[0]["REQID"];
                                    dtImage.Rows[DocIndex]["DocType"] = ddlType.SelectedValue.ToString();
                                    dtImage.Rows[DocIndex]["FileType"] = file.GetExtension().ToString();
                                    dtImage.Rows[DocIndex]["uploadId"] = int.Parse(dtImage.Rows[DocIndex - 1]["uploadId"].ToString()) + 1;
                                    dtImage.Rows[DocIndex]["Data"] = fileData;
                                    //dtImage.Rows[DocIndex]["DATAstream"] = streamdata;
                                    
                                }
                            }

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Dec 03, 2013 03:05 AM

Refer

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/uploading-a-file-using-wcf-rest-service/