这几天有个项目需要用到文件的操作,复习了一下有关的资料整合一下:
1,将Image对象输出到response输出流中:
//写入到响应流 smallimg.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); //保存到本地 smallimg.Save(context.Request.MapPath(smallimgname));
2,读取和发送字节数组
//将表单中的文件用字节数组的形式发送到制定的url地址中WebClient wb = new WebClient(); wb.UploadData(urladdress, FileUpload1.FileBytes); //接收传来的文件 using (FileStream fs = File.OpenWrite(@"h:\" + id + ext)) { //context.Request.InputStream就是Web服务器wc.UploadData第二个参数的数据流 context.Request.InputStream.CopyTo(fs); }//保存图片数据 验证码中提取System.IO.MemoryStream stream = new System.IO.MemoryStream();image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);//输出图片流context.Response.Clear();context.Response.ContentType = "image/jpeg";context.Response.BinaryWrite(stream.ToArray());
时间: 2024-08-06 16:06:31