public void ProcessRequest(HttpContext context) { string fileIdentity = context.Request.QueryString["FileIdentity"]; if (fileIdentity == null || fileIdentity.Trim().Length <= 0) { throw new ApplicationException("There is no 'FileIdentity'."); } fileIdentity = Encoding.UTF8.GetString(Convert.FromBase64String(fileIdentity)); if (context.Request.QueryString["Action"] == "cancel") { FileUploadManager.DeleteFile(fileIdentity); return; } long startPosition = 0; string startStr = context.Request.QueryString["StartPosition"]; if (startStr == null || startStr.Trim().Length <= 0 || !long.TryParse(startStr, out startPosition) || startPosition < 0) { throw new ApplicationException("There is no 'StartPosition' or 'StartPosition' is invalid."); } string filePath = Path.Combine(FileUploadManager.BaseFolder, fileIdentity); string folderPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } Stream stream = context.Request.InputStream; using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) { fs.Seek(startPosition, SeekOrigin.Begin); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { fs.Write(buffer, 0, bytesRead); } fs.Close(); } context.Response.Write(fileIdentity); }
/// <summary> ///是否为图片 /// </summary> /// <param name="fileIdentity"></param> public static bool IsImages(string fileIdentity) { var result = FileUploadManager.FileExists(fileIdentity); if (!result) { return(false); } var curentByte = FileUploadManager.GetFileData(fileIdentity, 0, 3); if (curentByte.Length < 3) { return(false); } //读取文件前三个字节确定文件后缀 var fileType = curentByte[0].ToString() + curentByte[1].ToString() + curentByte[2].ToString(); return(fileType.Contains(JPG.ToString())); }