private void UploadWholeFile(HttpRequestBase request, ViewDataUploadFilesResult statuses,int appser) { for (int i = 0; i < request.Files.Count; i++) { var file = request.Files[i]; var fullPath = Path.Combine(StorageRoot, Path.GetFileName(file.FileName)); file.SaveAs(fullPath); Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter photoadp = new Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter(); statuses.name = file.FileName; statuses.size = file.ContentLength; statuses.contentType = file.ContentType; statuses.url = "/Files/" + file.FileName; statuses.deleteUrl = "";//"/Product/Delete?id=" + file.FileName; //statuses.thumbnailUrl = @"data:image/png;base64," + EncodeFile(fullPath); statuses.deleteType = "GET"; int ret_app_dser; ret_app_dser = Convert.ToInt32(photoadp.InsertPhoto(appser, statuses.name, statuses.size, statuses.url, fullPath, statuses.deleteUrl, statuses.deleteType, statuses.contentType)); statuses.deleteUrl = "/Product/Delete?id=" + Convert.ToString(ret_app_dser); photoadp.UpdateDeleteURL(statuses.deleteUrl, ret_app_dser); /* statuses.Add(new ViewDataUploadFilesResult() { name = file.FileName, size = file.ContentLength, type = file.ContentType, url = "/Home/Download/" + file.FileName, delete_url = "/Home/Delete/" + file.FileName, thumbnail_url = @"data:image/png;base64," + EncodeFile(fullPath), delete_type = "GET", }); * */ } }
private void UploadPartialFile(string fileName, HttpRequestBase request, ViewDataUploadFilesResult statuses,int appser) { if (request.Files.Count != 1) throw new HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request"); var file = request.Files[0]; var inputStream = file.InputStream; var fullName = Path.Combine(StorageRoot, Path.GetFileName(fileName)); using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write)) { var buffer = new byte[1024]; var l = inputStream.Read(buffer, 0, 1024); while (l > 0) { fs.Write(buffer, 0, l); l = inputStream.Read(buffer, 0, 1024); } fs.Flush(); fs.Close(); } Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter photoadp = new Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter(); statuses.name = fileName; statuses.size = file.ContentLength; statuses.contentType = file.ContentType; statuses.url = "/Files/" + fileName; statuses.deleteUrl = ""; //"/Product/Delete?id=" + fileName; //statuses.thumbnailUrl = @"data:image/png;base64," + EncodeFile(fullName); statuses.deleteType = "GET"; int ret_app_dser; ret_app_dser = Convert.ToInt32(photoadp.InsertPhoto(appser, statuses.name, statuses.size, statuses.url, fullName, statuses.deleteUrl, statuses.deleteType, statuses.contentType)); statuses.deleteUrl = "/Product/Delete?id=" + Convert.ToString(ret_app_dser); photoadp.UpdateDeleteURL(statuses.deleteUrl, ret_app_dser); /* statuses.Add(new ViewDataUploadFilesResult() { name = fileName, size = file.ContentLength, type = file.ContentType, url = "/Home/Download/" + fileName, delete_url = "/Home/Delete/" + fileName, thumbnail_url = @"data:image/png;base64," + EncodeFile(fullName), delete_type = "GET", }); * */ }
// 取得某產品分類下所有被啟用的產品列表 Api public ActionResult getProductData(string appSer) { System.Diagnostics.Debug.WriteLine("[Get] GetProductData AppSer >>> " + appSer); // 將 arrayList 掛到 files 底下, 並轉成json回傳前端 Hashtable myHT = new Hashtable(); // 1. 先撈資料庫 File Table 的資料 (SQL) //----> 程式碼 Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter photoadp = new Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter(); DataTable dt = photoadp.GetPhotoList(Convert.ToInt32(appSer)); ArrayList list = new ArrayList(); // 2. 使用迴圈將每一筆 row 取出 foreach (DataRow drow in dt.Rows) { var fileItem = new ViewDataUploadFilesResult(); fileItem.name = wf.tos(drow["file_name"]); fileItem.size = wf.toi(drow["file_size"]); fileItem.contentType = wf.tos(drow["content_type"]); fileItem.url = wf.tos(drow["download_url"]); fileItem.deleteUrl = wf.tos(drow["delete_url"]); //"/Product/Delete?id=" + appSer; // file table的 App_Ser // fileItem.thumbnailUrl = @"data:image/png;base64," + EncodeFile(wf.tos(drow["thumbnail_url"])); fileItem.deleteType = wf.tos(drow["delete_type"]); list.Add(fileItem); } myHT.Add("files", list); // 1. 資料庫連線 SqlConnection cn = new SqlConnection(_connectionString); // 2. SQL 指令 string sql = "select a.app_ser,a.ProductID as prod_no,a.ProName as prod_name,a.pro_desc as prod_desc, a.prod_price, a.prod_special_price, a.prod_feature, b.pro_class_name from product a left join Product_Class b on a.prod_class_id=b.app_ser where a.pro_active='on' and a.app_ser=@id"; //string sql = "select app_ser,ProductID,ProName,'' as prod_desc,SalePrice,'' as prod_special_price,'' as prod_feature,'' as prod_class from product"; cn.Open(); SqlCommand comm = new SqlCommand(sql, cn); comm.Parameters.Clear(); comm.Parameters.AddWithValue("@id", appSer); // 2. SqlDataAdapter & DataSet DataSet ds = new DataSet(); using (cn) { SqlDataAdapter adapter = new SqlDataAdapter(comm); adapter.Fill(ds); DataTable myDataTable = ds.Tables[0]; cn.Close(); cn.Dispose(); myHT.Add("productItem", myDataTable); string obj_json = JsonConvert.SerializeObject(myHT); return Content(obj_json, "application/json"); } }
public ActionResult UploadFiles(FormCollection formCollection) { // 1. 接收前端傳入的資料 System.Diagnostics.Debug.WriteLine("[Post] appSer >>> " + formCollection["appSer"]); int appser; appser = Convert.ToInt32(formCollection["appSer"]); // 2. 由appSer 找到 Master產品資料 //----> 程式碼 // 3. 接下來做檔案上傳動作 ArrayList list = new ArrayList(); foreach (string file in Request.Files) { var statuses = new ViewDataUploadFilesResult(); var headers = Request.Headers; // 4. 檔案上傳到伺服器 if (string.IsNullOrEmpty(headers["X-File-Name"])) { UploadWholeFile(Request, statuses, appser); //於此function 寫入File 資料表, 紀錄儲存路徑/檔案大小, 檔案名稱 } else { UploadPartialFile(headers["X-File-Name"], Request, statuses, appser);//於此function 寫入File 資料表, 紀錄儲存路徑/檔案大小, 檔案名稱 } list.Add(statuses); } //Response.End(); // 6. 回傳Json資料;將list 掛在files Hashtable myHT = new Hashtable(); myHT.Add("files", list); /* JsonResult result = Json(statuses); result.ContentType = "text/json"; */ //string obj_json = JsonConvert.SerializeObject(myHT); //return Content(obj_json, "application/json"); return Json(myHT); }
public ActionResult GetFileList(string appSer) { System.Diagnostics.Debug.WriteLine("[Get] UploadFiles AppSer >>> " + appSer); ArrayList list = new ArrayList(); // 1. 先撈資料庫 File Table 的資料 (SQL) //----> 程式碼 Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter photoadp = new Models.ShopCarDatasetTableAdapters.Product_PhotoTableAdapter(); DataTable dt = photoadp.GetPhotoList(Convert.ToInt32(appSer)); // 2. 取得DataSet中的 DataTabe資訊 //----> 程式碼 // 3. 使用迴圈將每一筆 row 取出 foreach (DataRow drow in dt.Rows) { // ----> 新增每一個 statuses 將取出的row 資料塞入 var statuses = new ViewDataUploadFilesResult(); statuses.name = wf.tos(drow["file_name"]); statuses.size =wf.toi(drow["file_size"]); statuses.contentType = wf.tos(drow["content_type"]); statuses.url = wf.tos(drow["download_url"]); statuses.deleteUrl = wf.tos(drow["delete_url"]); //"/Product/Delete?id=" + appSer; // file table的 App_Ser //statuses.thumbnailUrl = @"data:image/png;base64," + EncodeFile(wf.tos(drow["thumbnail_url"])); statuses.deleteType = wf.tos(drow["delete_type"]); list.Add(statuses); //var statuses2 = new ViewDataUploadFilesResult(); //statuses2.name = "09.jpg"; //statuses2.size = 123456; //statuses2.contentType = "image/jpeg"; //statuses2.url = "/Files/09.jpg"; //statuses2.deleteUrl = "/Product/Delete?id=" + "123455"; // file table的 App_Ser //statuses2.thumbnailUrl = "/Files/09.jpg"; //statuses2.deleteType = "GET"; //list.Add(statuses2); } // 將 arrayList 掛到 files 底下, 並轉成json回傳前端 Hashtable myHT = new Hashtable(); myHT.Add("files", list); return Json(myHT, JsonRequestBehavior.AllowGet); }