Exemple #1
0
        public Result <UploadDataResult> Upload(UploadData data)
        {
            Result <UploadDataResult> result = new Result <UploadDataResult>();

            try
            {
                if (data != null && !string.IsNullOrWhiteSpace(data.data))
                {
                    string ffn = "data:image/";
                    ///data:image/jpeg;base64,
                    int    li  = data.data.IndexOf(';');
                    string lfn = data.data.Substring(ffn.Length, li - ffn.Length);
                    if (string.IsNullOrWhiteSpace(lfn))
                    {
                        lfn = "jpeg";
                    }
                    li        = data.data.IndexOf(',');
                    data.data = data.data.Substring(++li);
                    byte[] fData = Pub.DeBase64ToBytes(data.data);
                    if (fData != null && fData.Length > 0)
                    {
                        string df = "Resource/Images/r";
                        string fp = HostingEnvironment.MapPath("~/" + df);
                        if (!Directory.Exists(fp))
                        {
                            Directory.CreateDirectory(fp);
                        }
                        string rf = Pub.ID() + "." + lfn;
                        string f  = "/" + rf;
                        fp += f;
                        f   = df + "/" + rf;
                        FileStream imgFile = System.IO.File.Open(fp, FileMode.OpenOrCreate);
                        imgFile.Write(fData, 0, fData.Length);
                        imgFile.Flush();
                        imgFile.Close();
                        fData            = null;
                        result.Data      = new UploadDataResult();
                        result.Data.name = f;
                        result.Code      = R.Ok;
                    }
                }
            }
            catch (Exception)
            {
                result.Msg = "保存文件失败";
            }

            return(result);
        }