A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 夏华成 中级黑马   /  2012-5-19 09:41  /  4007 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

以下是本人QQ空间早期日记. 都是自己实际工作中用到的(分享一下)


申明wcf 服务
 
  1. [WebMethod]
  2. public void StoreFileAdvanced(string fileName, string fileNamex, byte[] data, int dataLength, string parameters, bool firstChunk, bool lastChunk, string path, int w, int h, string lei1, string lei2, string Pathparent)
  3. {
  4. string uploadFolder = Pathparent;
  5. string tempFileName=fileName;//大文件名
  6. string tempFileNamex=fileNamex;//小文件名


  7. string webpath=@HostingEnvironment.ApplicationPhysicalPath;

  8. string fileUrlPath = webpath + "/" + uploadFolder + "/" + path + "/" + DateTime.Now.ToString("yyyyMM");//文件要存储路径
  9. if (firstChunk)
  10. {


  11. if (!Directory.Exists(fileUrlPath))
  12. {
  13. Directory.CreateDirectory(fileUrlPath); //创建文件夹
  14. }
  15. //Delete temp file
  16. if (File.Exists(fileUrlPath + "/" + fileName))
  17. File.Delete(fileUrlPath + "/" + fileName);

  18. //Delete target file
  19. if (File.Exists(fileUrlPath + "/" + fileNamex))
  20. File.Delete(fileUrlPath + "/" + fileNamex);

  21. }


  22. FileStream fs = File.Open(fileUrlPath + "/" + fileName, FileMode.Append);
  23. fs.Write(data, 0, dataLength);
  24. fs.Close();

  25. if (lastChunk)
  26. {
  27. if (w == 0 || h == 0)
  28. {

  29. }
  30. else
  31. {
  32. if (tempFileName.IndexOf(".jpg") > -1 || tempFileName.IndexOf(".png") > -1 || tempFileName.IndexOf(".bmp") > -1 || tempFileName.IndexOf(".gif") > -1)
  33. {
  34. try
  35. {
  36. MakeThumbnail(fileUrlPath + "/" + tempFileName, fileUrlPath + "/" + tempFileNamex, w, h);
  37. }
  38. catch (Exception ex)
  39. {
  40. //Delete temp file
  41. if (File.Exists(fileUrlPath + "/" + fileName))
  42. File.Delete(fileUrlPath + "/" + fileName);

  43. //Delete target file
  44. if (File.Exists(fileUrlPath + "/" + fileNamex))
  45. File.Delete(fileUrlPath + "/" + fileNamex);
  46. MakeThumbnail(fileUrlPath + "/" + tempFileName, fileUrlPath + "/" + tempFileNamex, w, h);
  47. }
  48. }
  49. }
  50. }

  51. }

复制代码
引用 wcf 服务
  1. _client = new uploadSoapClient();
  2. _client.StoreFileAdvancedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_StoreFileAdvancedCompleted);

  3.    void _client_StoreFileAdvancedCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  4. {
  5. //检查WEB服务是否存在错误
  6. if (e.Error != null)
  7. {
  8. //当错误时放弃上传
  9. _file.State = Constants.FileStates.错误;
  10. }
  11. else
  12. {
  13. //如果文件未取消上传的话,则继续上传
  14. if (!_file.IsDeleted)
  15. UploadAdvanced();
  16. }
  17. }

  18.   private void UploadAdvanced()
  19. {
  20. // _file 这里是一个文件对像
  21. byte[] buffer = new byte[4096*4];
  22. int bytesRead = _file.FileStream.Read(buffer, 0, buffer.Length);

  23. //文件是否上传完毕?
  24. if (bytesRead != 0)
  25. {
  26. _dataSent += bytesRead;

  27. if (_dataSent == _dataLength)
  28. _lastChunk = true;//是否是最后一块数据

  29. //上传当前数据块
  30. _client.StoreFileAdvancedAsync(_file.FileName,_file.FileNamex, buffer, bytesRead, _initParams, _firstChunk, _lastChunk,_file.Path,_file.W,_file.H,_file.Lei1,_file.Lei2,_file.Pathparent);


  31. //在第一条消息之后一直为false
  32. _firstChunk = false;

  33. //通知上传进度修改
  34. OnProgressChanged();
  35. }
  36. else
  37. {
  38. //当上传完毕后
  39. _file.FileStream.Dispose();
  40. _file.FileStream.Close();

  41. _client.ChannelFactory.Close();
  42. }

  43. }

复制代码
如果上传的是图片。关于生成缩微图
  1. protected virtual void MakeThumbnail(string originalImagePath, string thumbnailPath, int width1, int height1)
  2. {
  3. System.Drawing.Image original_image = System.Drawing.Image.FromFile(originalImagePath);



  4. // Calculate the new width and height
  5. int width = original_image.Width;
  6. int height = original_image.Height;
  7. int target_width = width1;
  8. int target_height = height1;
  9. int new_width, new_height;

  10. float target_ratio = (float)target_width / (float)target_height;
  11. float image_ratio = (float)width / (float)height;

  12. if (target_ratio > image_ratio)
  13. {
  14. new_height = target_height;
  15. new_width = (int)Math.Floor(image_ratio * (float)target_height);
  16. }
  17. else
  18. {
  19. new_height = (int)Math.Floor((float)target_width / image_ratio);
  20. new_width = target_width;
  21. }

  22. new_width = new_width > target_width ? target_width : new_width;
  23. new_height = new_height > target_height ? target_height : new_height;


  24. int paste_x = 0; //(target_width - new_width) / 2;
  25. int paste_y = 0; //(target_height - new_height) / 2;


  26. //新建一个bmp图片
  27. System.Drawing.Image bitmap = new System.Drawing.Bitmap(new_width, new_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

  28. //新建一个画板
  29. Graphics g = System.Drawing.Graphics.FromImage(bitmap);

  30. //设置高质量插值法
  31. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

  32. //设置高质量,低速度呈现平滑程度
  33. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

  34. //清空画布并以透明背景色填充
  35. g.Clear(System.Drawing.Color.White);

  36. //在指定位置并且按指定大小绘制原图片的指定部分
  37. g.DrawImage(original_image, paste_x, paste_y, new_width, new_height);

  38. try
  39. {
  40. //以jpg格式保存缩略图
  41. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  42. }
  43. catch (System.Exception e)
  44. {
  45. throw e;
  46. }
  47. finally
  48. {
  49. original_image.Dispose();
  50. bitmap.Dispose();
  51. g.Dispose();
  52. }
  53. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
宋天琪 + 2

查看全部评分

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马