前台form-data格式 上傳文件


 public HttpResponseMessage UpLoad()
        {
            // "請求內容不是表單形式";
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                var json = PostModel("請求內容不是表單形式");
                return json;
            }
            if (HttpContext.Current.Request.Files.Count > 0)
            {
                Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
                try
                {                
                    var file = HttpContext.Current.Request.Files[0];//獲取文件
                    //獲取前端from-data表單形式中的參數
                    HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//獲取傳統context
                    HttpRequestBase request = context.Request;//定義傳統request對象                  
                    string fname = file.FileName;//獲取文件名稱
                    string SavePath = HttpContext.Current.Server.MapPath(string.Format("~/{0}", "合同附件"));
                    if (!Directory.Exists(SavePath))
                    {
                        Directory.CreateDirectory(SavePath);
                    }
                    string fullPathUrl = Path.Combine(SavePath, fname);
                    file.SaveAs(fullPathUrl);
                    #region 賦值
                    Appendix app = new Appendix();
                    app.value = 1;
                    app.file_url = fullPathUrl;
                    app.file_name = fname;
                    app.cid = Convert.ToInt32(request.Form["cid"]);
                    #endregion
                    VMAppendix vM = new VMAppendix();
                    //添加文件
                    var result = vM.CUDAppendix(app);
                    var json = PostModel(result);
                    return json;
                }
                catch (Exception ex)
                {
                    log.log(" 錯誤提示:" + ex.ToString());
                    throw ex;
                }
            }
            return PostModel("無文件!");
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM