コード例 #1
0
        public UploadResult UploadFile(FileUploadMessage request)
        {
            UploadResult result = new UploadResult();            
            Stream sourceStream = request.FileData;
            FileStream targetStream = null;     
            //判断数据流是否有
            if (!sourceStream.CanRead)
            {                
                result.Result = false;
                return result;
            }            
            string directory;
            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["SavePath"]))
                directory = ConfigurationManager.AppSettings["SavePath"]+@"\";
            else
                directory = AppDomain.CurrentDomain.BaseDirectory;
            string dateString = DateTime.Now.Year.ToString() + @"\";
            string extension = Path.GetExtension(request.FileName);
            Guid guid = Guid.NewGuid();            
            string fileName = guid.ToString() + extension;
            directory = directory + dateString;
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            string filePath = Path.Combine(directory, fileName);
            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //read from the input stream in 4K chunks
                //and save to output stream
                const int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
                result.Result = true;
            }
            try
            {
                Image img = Image.FromFile(filePath);//检测是否为图片
                if (img.Height < 1 || img.Width < 1)
                {
                    File.Delete(filePath);
                    result.Result = false;
                    return result;
                }
            }
            catch (Exception)
            {
                File.Delete(filePath);
                result.Result = false;
            }           
            return result;
        }
コード例 #2
0
        public void UploadFile(FileUploadMessage request)
        {
            string     uploadFolder = @"C:\kkk\";
            string     savaPath     = request.SavePath;
            string     dateString   = DateTime.Now.ToShortDateString() + @"\";
            string     fileName     = request.FileName;
            Stream     sourceStream = request.FileData;
            FileStream targetStream = null;

            if (!sourceStream.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
            if (savaPath == null)
            {
                savaPath = @"Photo\";
            }
            if (!savaPath.EndsWith("\\"))
            {
                savaPath += "\\";
            }

            uploadFolder = uploadFolder + savaPath + dateString;
            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            string filePath = Path.Combine(uploadFolder, fileName);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //read from the input stream in 4K chunks
                //and save to output stream
                const int bufferLen = 4096;
                byte[]    buffer    = new byte[bufferLen];
                int       count     = 0;
                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
        }
コード例 #3
0
        public void UploadFile(FileUploadMessage request)
        {
            string uploadFolder = @"C:\kkk\";
            string savaPath = request.SavePath;
            string dateString = DateTime.Now.ToShortDateString() + @"\";
            string fileName = request.FileName;
            Stream sourceStream = request.FileData;
            FileStream targetStream = null;

            if (!sourceStream.CanRead)
            {
                throw new Exception("数据流不可读!");
            }
            if (savaPath == null) savaPath = @"Photo\";
            if (!savaPath.EndsWith("\\")) savaPath += "\\";

            uploadFolder = uploadFolder + savaPath + dateString;
            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            string filePath = Path.Combine(uploadFolder, fileName);
            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //read from the input stream in 4K chunks
                //and save to output stream
                const int bufferLen = 4096;
                byte[] buffer = new byte[bufferLen];
                int count = 0;
                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
        }
コード例 #4
0
        //[OperationContract]
        //[WebInvoke(Method ="GET", ResponseFormat =WebMessageFormat.Json)]
        public FileReturnMessage UploadFile(FileUploadMessage request)
        {
            bool   IsOk         = true;
            string uploadFolder = @"C:\kkk\";
            string savaPath     = request.SavePath;
            string dateString   = DateTime.Now.ToShortDateString() + @"\";
            string fileName     = request.FileName;
            long   tranter      = request.Tranter;
            long   tranters     = request.Tranters;
            int    length       = request.length;
            string fileNamePath = "";

            if (fileName.Contains("."))
            {
                fileNamePath = fileName.Substring(0, fileName.LastIndexOf("."));
            }
            else
            {
                fileNamePath = fileName;
            }
            Stream     sourceStream = request.FileData;
            FileStream targetStream = null;

            if (!sourceStream.CanRead)
            {
                IsOk = false;
                throw new Exception("数据流不可读!");
            }
            if (savaPath == null)
            {
                savaPath = @"Photo\";
            }
            if (!savaPath.EndsWith("\\"))
            {
                savaPath += "\\";
            }

            uploadFolder = uploadFolder + savaPath + fileNamePath;
            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }

            string filePath = Path.Combine(uploadFolder, fileName);

            using (targetStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None))
            {
                //targetStream.Write(Buffer, 0, length);
                //read from the input stream in 4K chunks
                //and save to output stream
                const int bufferLen = 4096;
                byte[]    buffer    = new byte[bufferLen];
                int       count     = 0;
                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {
                    targetStream.Write(buffer, 0, count);
                }
                targetStream.Close();
                sourceStream.Close();
            }
            if (tranter == tranters)
            {
                string Str1 = File.ReadAllText(filePath);
                return(new FileReturnMessage {
                    IsOk = !IsOk, FileName = fileName, MsgData = Str1, Tranter = tranter, Tranters = tranters
                });
            }
            return(new FileReturnMessage {
                IsOk = IsOk, FileName = fileName, MsgData = "上传成功", Tranter = tranter, Tranters = tranters
            });
        }