示例#1
0
        //
        #region 通过编辑器上传图片
        public void ImageUpLoadEditor()
        {
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            string    callback = System.Web.HttpContext.Current.Request["callback"];
            string    editorId = System.Web.HttpContext.Current.Request["editorid"];
            Hashtable infoList = new Hashtable();
            //-----------------------------
            bool   uploadSuccess = true;
            string uploadMsg     = "";
            //=================================
            string UpLoadFileUrl = "";//
            string mobilePhone   = "defalut";
            //
            int    _sizeMax = 500 * (1024 * 1024);//500Mb
            string myPath   = "";
            string myFolder = "";
            int    RandKey  = 1000;
            string filePath = "";
            int    mySize   = 0;  //文件大小
            string fileExt  = ""; //文件类型

            if (User.Identity.IsAuthenticated)
            {
                mobilePhone = User.Identity.Name;
                //文件夹不存在则创建
                HttpFileCollection _fileList = System.Web.HttpContext.Current.Request.Files;
                int fileCount = _fileList.Count;
                if (fileCount > 0)
                {
                    for (int i = 0; i < fileCount; i++)
                    {
                        try
                        {
                            System.Web.HttpPostedFile postedFile = _fileList[i];
                            string fileName = postedFile.FileName;
                            if (fileName != string.Empty)
                            {
                                mySize = postedFile.ContentLength;
                                if (mySize >= _sizeMax) //判断上传的大小
                                {
                                    uploadSuccess = false;
                                    uploadMsg     = "文件大于" + (_sizeMax / (1024 * 1024)).ToString() + "MB";
                                    // "文件太大";
                                }
                                else if (mySize > 10)
                                {
                                    fileExt = fileName.Substring(fileName.LastIndexOf(".")).ToLower();    //图片类型
                                    folderActions myForderObject = new folderActions();
                                    //
                                    myPath = StaticVarClass.ImageFolderCfg + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                                    #region 判断文件类型
                                    switch (fileExt)
                                    {
                                    case ".jpg":
                                    case ".png":
                                    case ".gif":
                                    case ".jpeg":
                                    case ".bmp":
                                        myPath = StaticVarClass.ImageFolderCfg;
                                        break;

                                    case ".mp3":
                                    case ".aac":
                                        myPath = StaticVarClass.AudioFolderCfg;
                                        break;

                                    case ".mp4":
                                        myPath = StaticVarClass.VideoFolderCfg;
                                        break;

                                    default:
                                        uploadSuccess = false;
                                        uploadMsg     = "文件类型错误";
                                        break;
                                    }
                                    if (uploadSuccess)
                                    {
                                        myPath = myPath + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                                        #endregion
                                        //
                                        myForderObject.setFolderFullName = myPath;
                                        myForderObject.CreateFolder();
                                        myFolder = myForderObject.getFolderRealPath;
                                        Random ran = new Random(unchecked ((int)DateTime.Now.Ticks));
                                        RandKey  = ran.Next(1, 9999);
                                        fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + RandKey.ToString();
                                        filePath = myFolder + "/" + fileName + fileExt;

                                        //
                                        if (System.IO.File.Exists(filePath))
                                        {
                                            System.IO.File.Delete(filePath);
                                        }
                                        postedFile.SaveAs(filePath);
                                        UpLoadFileUrl = myPath + "/" + fileName + fileExt;
                                        break;
                                    }
                                }
                                else
                                {
                                    uploadSuccess = false;
                                    uploadMsg     = "文件为空";
                                }
                            }
                            else
                            {
                                uploadSuccess = false;
                                uploadMsg     = "文件为空";
                            }
                        }
                        catch
                        {
                            uploadSuccess = false;
                            uploadMsg     = "发生未知错误";
                        }
                    }
                }
                else
                {
                    uploadSuccess = false;
                    uploadMsg     = "请选择要上传的文件";
                }
            }
            else
            {
                uploadSuccess = false;
                uploadMsg     = "授权码无效";
            }
            //==============================
            if (uploadSuccess)
            {
                infoList.Add("state", "SUCCESS");
                infoList.Add("url", StaticVarClass.myDomain + UpLoadFileUrl);
                infoList.Add("originalName", "children.png");
                infoList.Add("name", "45c3bdfa-46ac-49cd-b299-889940307e0e.png");
                infoList.Add("size", mySize);
                infoList.Add("type", fileExt);
            }
            else
            {
                infoList.Add("state", "FAIL");
                infoList.Add("url", StaticVarClass.myDomain + UpLoadFileUrl);
                infoList.Add("originalName", "children.png");
                infoList.Add("name", "45c3bdfa-46ac-49cd-b299-889940307e0e.png");
                infoList.Add("size", mySize);
                infoList.Add("type", fileExt);
            }
            string json = BuildJson(infoList);
            System.Web.HttpContext.Current.Response.ContentType = "text/html";
            if (callback != null)
            {
                System.Web.HttpContext.Current.Response.Write(String.Format("<script>{0}(JSON.parse(\"{1}\"));</script>", callback, json));
            }
            else
            {
                System.Web.HttpContext.Current.Response.Write(json);
            }
        }
示例#2
0
        public string FileUpLoad()
        {
            string UpLoadFileUrl = "";//
            string mobilePhone   = "defalut";
            //
            int    _sizeMax = 500 * (1024 * 1024);//500Mb
            string myPath   = "";
            string myFolder = "";
            int    RandKey  = 1000;
            string filePath = "";

            if (User.Identity.IsAuthenticated)
            {
                mobilePhone = User.Identity.Name;
                //文件夹不存在则创建
                HttpFileCollection _fileList = System.Web.HttpContext.Current.Request.Files;
                int fileCount = _fileList.Count;
                if (fileCount > 0)
                {
                    for (int i = 0; i < fileCount; i++)
                    {
                        try
                        {
                            System.Web.HttpPostedFile postedFile = _fileList[i];
                            string fileName = postedFile.FileName;
                            if (fileName != string.Empty)
                            {
                                int mySize = postedFile.ContentLength;
                                if (mySize >= _sizeMax) //判断上传的大小
                                {
                                    return("文件太大");
                                }
                                else if (mySize > 10)
                                {
                                    string        fileExt        = fileName.Substring(fileName.LastIndexOf(".")).ToLower(); //图片类型
                                    folderActions myForderObject = new folderActions();
                                    //
                                    myPath = StaticVarClass.ImageFolderCfg + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                                    #region 判断文件类型
                                    switch (fileExt)
                                    {
                                    case ".jpg":
                                    case ".png":
                                    case ".gif":
                                    case ".jpeg":
                                    case ".bmp":
                                        myPath = StaticVarClass.ImageFolderCfg;
                                        break;

                                    case ".mp3":
                                    case ".aac":
                                        myPath = StaticVarClass.AudioFolderCfg;
                                        break;

                                    case ".mp4":
                                        myPath = StaticVarClass.VideoFolderCfg;
                                        break;

                                    default:
                                        return("文件类型错误");
                                    }
                                    myPath = myPath + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                                    #endregion
                                    //
                                    myForderObject.setFolderFullName = myPath;
                                    myForderObject.CreateFolder();
                                    myFolder = myForderObject.getFolderRealPath;
                                    Random ran = new Random(unchecked ((int)DateTime.Now.Ticks));
                                    RandKey  = ran.Next(1, 9999);
                                    fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + RandKey.ToString();
                                    filePath = myFolder + "/" + fileName + fileExt;

                                    //
                                    if (System.IO.File.Exists(filePath))
                                    {
                                        System.IO.File.Delete(filePath);
                                    }
                                    postedFile.SaveAs(filePath);
                                    UpLoadFileUrl = myPath + "/" + fileName + fileExt;
                                    break;
                                }
                            }
                        }
                        catch
                        {
                            return("发生未知错误");
                        }
                    }
                }
                else
                {
                    return("请选择要上传的文件");
                }
            }
            else
            {
                return("授权码无效");//授权码无效
            }
            return(UpLoadFileUrl);
        }
示例#3
0
        public JsonResult UpdateFile(string filetype, string upfilename)
        {
            StatusData myStatusData = new StatusData();//返回的类型

            #region 检查授权
            string           mobilePhone        = "";
            AuthCodeInstance myAuthCodeInstance = new AuthCodeInstance();
            AuthCodeClass    myAuthCodeClass    = new AuthCodeClass();
            bool             checkAuthCodeBool  = myAuthCodeClass.checkAuthCode(out myAuthCodeInstance);
            #endregion
            //
            string myPath   = "";
            string fileExt  = "";
            string myFolder = "";
            int    RandKey  = 1000;
            string fileName = "";
            string filePath = "";

            //
            #region 判断是否续传
            string upfilenamefullpath = "";
            bool   xuchuan            = false;
            //
            if (!String.IsNullOrWhiteSpace(upfilename))
            {
                upfilenamefullpath = Path.Combine(HttpRuntime.AppDomainAppPath, upfilename);
                if (System.IO.File.Exists(upfilenamefullpath))
                {
                    xuchuan = true;
                }
                else
                {
                    xuchuan = false;
                }
            }
            #endregion
            //
            if (checkAuthCodeBool)
            {
                mobilePhone = myAuthCodeInstance.mobilePhone;
                try
                {
                    if (!xuchuan)
                    {
                        #region  是续传,获得文件相关属性
                        folderActions myForderObject = new folderActions();
                        //
                        myPath  = StaticVarClass.ImageFolderCfg + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                        fileExt = filetype;
                        #region 判断文件类型
                        if (!String.IsNullOrWhiteSpace(fileExt))
                        {
                            fileExt = fileExt.ToLower();
                        }
                        else
                        {
                            fileExt = "";
                        }
                        switch (fileExt)
                        {
                        case "jpg":
                        case "png":
                        case "gif":
                        case "jpeg":
                            myPath = StaticVarClass.ImageFolderCfg;
                            break;

                        case "mp3":
                        case "aac":
                            myPath = StaticVarClass.AudioFolderCfg;
                            break;

                        case "mov":
                        case "mp4":
                            myPath = StaticVarClass.VideoFolderCfg;
                            break;

                        default:
                            myStatusData.operateStatus = 400;
                            myStatusData.operateMsg    = "文件类型错误";
                            return(Json(myStatusData));
                        }
                        myPath = myPath + "/" + DateTime.Now.Year.ToString() + "/" + mobilePhone + "/" + DateTime.Now.Month.ToString();
                        #endregion
                        //
                        myForderObject.setFolderFullName = myPath;
                        myForderObject.CreateFolder();
                        myFolder = myForderObject.getFolderRealPath;
                        Random ran = new Random(unchecked ((int)DateTime.Now.Ticks));
                        RandKey  = ran.Next(1, 9999);
                        fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + RandKey.ToString();
                        filePath = myFolder + "/" + fileName + "." + fileExt;
                        #endregion
                    }
                    else
                    {//续传,直接用文件路径
                        filePath = upfilenamefullpath;
                    }
                    //
                    try
                    {
                        if ((!xuchuan) && (!System.IO.File.Exists(filePath)))
                        {
                            #region 新文件
                            using (Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                            {
                                //将字符信息写入文件系统
                                System.IO.Stream s = HttpContext.Request.InputStream;
                                if (s.Length > 10)
                                {
                                    int    count  = 0;
                                    byte[] buffer = new byte[1024];
                                    while ((count = s.Read(buffer, 0, 1024)) > 0)
                                    {
                                        stream.Write(buffer, 0, count);
                                    }
                                    s.Flush();
                                    s.Close();
                                    s.Dispose();

                                    UploadFileClass myUploadFileClass = new UploadFileClass();
                                    myUploadFileClass.filename = myPath + "/" + fileName + "." + fileExt;
                                    myUploadFileClass.fileurl  = myPath + "/" + fileName + "." + fileExt;
                                    //myUploadFileClass.fileurl = StaticVarClass.myDomain + myPath + "/" + fileName + "." + fileExt;
                                    myStatusData.dataTable     = myUploadFileClass;
                                    myStatusData.operateStatus = 200;
                                }
                                else
                                {
                                    myStatusData.operateMsg    = "文件为空";
                                    myStatusData.operateStatus = 0;
                                }
                            }
                            //
                            #endregion
                        }
                        else if (xuchuan)
                        {
                            #region 续传文件
                            using (Stream stream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                            {
                                //将字符信息写入文件系统
                                System.IO.Stream s = HttpContext.Request.InputStream;
                                if (s.Length > 10)
                                {
                                    int    count  = 0;
                                    byte[] buffer = new byte[1024];
                                    while ((count = s.Read(buffer, 0, 1024)) > 0)
                                    {
                                        stream.Write(buffer, 0, count);
                                    }
                                    s.Flush();
                                    s.Close();
                                    s.Dispose();
                                    //
                                    UploadFileClass myUploadFileClass = new UploadFileClass();
                                    myUploadFileClass.filename = upfilename;
                                    myUploadFileClass.fileurl  = upfilename;
                                    //myUploadFileClass.fileurl = StaticVarClass.myDomain + upfilename;
                                    myStatusData.dataTable     = myUploadFileClass;
                                    myStatusData.operateStatus = 200;
                                }
                                else
                                {
                                    myStatusData.operateMsg    = "文件为空";
                                    myStatusData.operateStatus = 0;
                                }
                            }
                            #endregion
                        }
                        else
                        {
                            myStatusData.operateStatus = 400;
                            myStatusData.operateMsg    = "文件已经存在";
                        }
                    }
                    catch
                    {//发生错误删除文件
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        myStatusData.operateStatus = -1;
                    }
                }
                catch
                {
                    myStatusData.operateStatus = -1;
                }
            }
            else
            {
                myStatusData.operateStatus = 5;
            }
            return(Json(myStatusData));
        }