以下是本人QQ空间早期日记. 都是自己实际工作中用到的(分享一下)
申明wcf 服务
- [WebMethod]
- 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)
- {
- string uploadFolder = Pathparent;
- string tempFileName=fileName;//大文件名
- string tempFileNamex=fileNamex;//小文件名
- string webpath=@HostingEnvironment.ApplicationPhysicalPath;
- string fileUrlPath = webpath + "/" + uploadFolder + "/" + path + "/" + DateTime.Now.ToString("yyyyMM");//文件要存储路径
- if (firstChunk)
- {
- if (!Directory.Exists(fileUrlPath))
- {
- Directory.CreateDirectory(fileUrlPath); //创建文件夹
- }
- //Delete temp file
- if (File.Exists(fileUrlPath + "/" + fileName))
- File.Delete(fileUrlPath + "/" + fileName);
- //Delete target file
- if (File.Exists(fileUrlPath + "/" + fileNamex))
- File.Delete(fileUrlPath + "/" + fileNamex);
- }
- FileStream fs = File.Open(fileUrlPath + "/" + fileName, FileMode.Append);
- fs.Write(data, 0, dataLength);
- fs.Close();
- if (lastChunk)
- {
- if (w == 0 || h == 0)
- {
- }
- else
- {
- if (tempFileName.IndexOf(".jpg") > -1 || tempFileName.IndexOf(".png") > -1 || tempFileName.IndexOf(".bmp") > -1 || tempFileName.IndexOf(".gif") > -1)
- {
- try
- {
- MakeThumbnail(fileUrlPath + "/" + tempFileName, fileUrlPath + "/" + tempFileNamex, w, h);
- }
- catch (Exception ex)
- {
- //Delete temp file
- if (File.Exists(fileUrlPath + "/" + fileName))
- File.Delete(fileUrlPath + "/" + fileName);
- //Delete target file
- if (File.Exists(fileUrlPath + "/" + fileNamex))
- File.Delete(fileUrlPath + "/" + fileNamex);
- MakeThumbnail(fileUrlPath + "/" + tempFileName, fileUrlPath + "/" + tempFileNamex, w, h);
- }
- }
- }
- }
- }
复制代码 引用 wcf 服务- _client = new uploadSoapClient();
- _client.StoreFileAdvancedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_StoreFileAdvancedCompleted);
- void _client_StoreFileAdvancedCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
- {
- //检查WEB服务是否存在错误
- if (e.Error != null)
- {
- //当错误时放弃上传
- _file.State = Constants.FileStates.错误;
- }
- else
- {
- //如果文件未取消上传的话,则继续上传
- if (!_file.IsDeleted)
- UploadAdvanced();
- }
- }
- private void UploadAdvanced()
- {
- // _file 这里是一个文件对像
- byte[] buffer = new byte[4096*4];
- int bytesRead = _file.FileStream.Read(buffer, 0, buffer.Length);
- //文件是否上传完毕?
- if (bytesRead != 0)
- {
- _dataSent += bytesRead;
- if (_dataSent == _dataLength)
- _lastChunk = true;//是否是最后一块数据
- //上传当前数据块
- _client.StoreFileAdvancedAsync(_file.FileName,_file.FileNamex, buffer, bytesRead, _initParams, _firstChunk, _lastChunk,_file.Path,_file.W,_file.H,_file.Lei1,_file.Lei2,_file.Pathparent);
- //在第一条消息之后一直为false
- _firstChunk = false;
- //通知上传进度修改
- OnProgressChanged();
- }
- else
- {
- //当上传完毕后
- _file.FileStream.Dispose();
- _file.FileStream.Close();
- _client.ChannelFactory.Close();
- }
- }
复制代码 如果上传的是图片。关于生成缩微图- protected virtual void MakeThumbnail(string originalImagePath, string thumbnailPath, int width1, int height1)
- {
- System.Drawing.Image original_image = System.Drawing.Image.FromFile(originalImagePath);
- // Calculate the new width and height
- int width = original_image.Width;
- int height = original_image.Height;
- int target_width = width1;
- int target_height = height1;
- int new_width, new_height;
- float target_ratio = (float)target_width / (float)target_height;
- float image_ratio = (float)width / (float)height;
- if (target_ratio > image_ratio)
- {
- new_height = target_height;
- new_width = (int)Math.Floor(image_ratio * (float)target_height);
- }
- else
- {
- new_height = (int)Math.Floor((float)target_width / image_ratio);
- new_width = target_width;
- }
- new_width = new_width > target_width ? target_width : new_width;
- new_height = new_height > target_height ? target_height : new_height;
- int paste_x = 0; //(target_width - new_width) / 2;
- int paste_y = 0; //(target_height - new_height) / 2;
- //新建一个bmp图片
- System.Drawing.Image bitmap = new System.Drawing.Bitmap(new_width, new_height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- //新建一个画板
- Graphics g = System.Drawing.Graphics.FromImage(bitmap);
- //设置高质量插值法
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- //清空画布并以透明背景色填充
- g.Clear(System.Drawing.Color.White);
- //在指定位置并且按指定大小绘制原图片的指定部分
- g.DrawImage(original_image, paste_x, paste_y, new_width, new_height);
- try
- {
- //以jpg格式保存缩略图
- bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- catch (System.Exception e)
- {
- throw e;
- }
- finally
- {
- original_image.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- }
复制代码 |