/// <summary> /// 上传图片 /// </summary> /// <param name="param"></param> /// <param name="message"></param> /// <returns></returns> public ImageUploadResult UploadImage(ImageUploadParameter param) { byte[] content; string shortName = ""; //1 获取拓展名 string ext = System.IO.Path.GetExtension(param.FileName).ToLower(); //1.2判断上传图片对象中的拓展名是否与实际拓展名相符 //若上传图片对象的拓展名为空或不行相符 if (param.FilenameExtension != null && param.FilenameExtension.Contains(ext)) { if (param.Stream.Length > param.MaxSize) { return(new ImageUploadResult { ErrorMessage = "图片大小超过指定大小" + param.MaxSize / (1024 * 1024) + "M,请重新选择", FullFilePath = shortName }); } else { using (BinaryReader reader = new BinaryReader(param.Stream)) { content = reader.ReadBytes((int)param.Stream.Length); } //**注意 调用FdfsClient的UploadFile时,拓展名不含.,需要手动去掉 //**注意返回值为:M00/00/00/wKgAcVjGSpSANp6XAAInn_BrY3k752.jpg shortName = FastDFSClient.UploadFile(Node, content, ext.Contains('.') ? ext.Substring(1) : ext); } } else { return(new ImageUploadResult { ErrorMessage = "文件类型不匹配", FullFilePath = shortName }); } return(new ImageUploadResult { FullFilePath = CompleteUpload(param.Stream, shortName), FileName = shortName, GroupName = Node.GroupName, Url = Host }); }
static void Main(string[] args) { // var config = FastDfsManager.GetConfigSection(); //测试第二种方式 //TrackerSection trackersSection = ConfigurationManager.GetSection("TrackerSection") as TrackerSection; //测试第三种方式 //MyFastDFSClient.Test(); StorageNode storageNode = null; var fileName = ""; if (!File.Exists("test.jpg")) { return; } FileStream streamUpload = new FileStream(@"test.jpg", FileMode.Open); Console.WriteLine("进行上传图片/文件 测试"); IUpload uploadFile = UploadFactory.Instance; //1 测试上传文件(非图片)——可行 var fileUploadPara = new FileUploadParameter() { FileName = "test.jpg", //Content= File.ReadAllBytes("test.jpg"), Stream = streamUpload }; //uploadFile.UploadFile(fileUploadPara); //2 测试上传图片 //**注意,maxSize以后改为读取配置文件 var fileUploadImage = new ImageUploadParameter(streamUpload, "test.jpg", null, 2 * 1024 * 1024) { }; //FilePath:http://192.168.0.113/group1/M00/00/00/wKgAcVjHVVKAGNhPAAInn_BrY3k026.jpg var result = uploadFile.UploadImage(fileUploadImage); Console.WriteLine("上传成功,上传图片为{0}", result.FullFilePath); Console.WriteLine("*******************************"); Console.WriteLine("1.直接获取刚才上传的文件的名称"); Console.WriteLine("2.手动收入文件名称"); Console.Write("请输入命令:"); var index = Console.ReadLine(); string filename_console = null; switch (index) { case "1": filename_console = result.FileName; Console.WriteLine("刚才上传的文件名为:{0}", filename_console); break; case "2": Console.WriteLine("请输入命令:"); Console.WriteLine("请输入文件路径准备下载指定文件"); filename_console = Console.ReadLine(); break; default: Console.WriteLine("输入有误!"); return; } IDownload downloadFile = DownloadFactory.Instance; Console.WriteLine("*******************************"); var result_download = downloadFile.GetTargetFile(new FileDownParameter() { FileName = filename_console }); Console.WriteLine("下载成功"); Console.ReadLine(); do { Console.WriteLine("\r\n1.Init"); Console.WriteLine("2.GetStorageNode"); Console.WriteLine("3.UploadFile"); Console.WriteLine("4.RemoveFile"); Console.Write("请输入命令:"); var cmd = Console.ReadLine(); switch (cmd) { case "1": ConnectionManager.InitializeForConfigSection(config); break; case "2": storageNode = MyFastDFSClient.GetStorageNode(config.GroupName); Console.WriteLine(storageNode.EndPoint); break; case "3": //暂时注释掉 fileName = MyFastDFSClient.UploadFile(storageNode, File.ReadAllBytes("test.jpg"), "jpg"); //此处不需要手动关闭了 //streamUpload.Close(); Console.WriteLine(fileName); break; case "4": MyFastDFSClient.RemoveFile(config.GroupName, fileName); break; } } while (true); }