zoukankan      html  css  js  c++  java
  • 图片上传/下载,DOC.xls文档下载的方法

    上传:

    Code

    下载

       private void ViewImage(string fileType, string fileName, bool forceDownload)
            
    {
                
    if (fileType == "application/msword" || fileType == "application/vnd.ms-excel")
                
    {
                    
    //Response.Redirect("UploadPic/" + fileName);
                    
    //return;
                    System.IO.FileStream r = new System.IO.FileStream(Server.MapPath("UploadPic/" + fileName), System.IO.FileMode.Open);
                    
    //设置基本信息 
                    Response.Buffer = false;
                    Response.AddHeader(
    "Connection""Keep-Alive");
                    Response.ContentType 
    = "application/octet-stream";
                    Response.AddHeader(
    "Content-Disposition""attachment;filename=" + System.IO.Path.GetFileName(Server.MapPath("UploadPic/" + fileName)));
                    Response.AddHeader(
    "Content-Length", r.Length.ToString());


                    
    while (true)
                    
    {
                        
    //开辟缓冲区空间 
                        byte[] buffer = new byte[1024];
                        
    //读取文件的数据 
                        int leng = r.Read(buffer, 01024);
                        
    if (leng == 0)//到文件尾,结束 
                            break;
                        
    if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入 
                            Response.BinaryWrite(buffer);
                        
    else
                        
    {
                            
    //读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块 
                            byte[] b = new byte[leng];
                            
    for (int i = 0; i < leng; i++)
                                b[i] 
    = buffer[i];
                            Response.BinaryWrite(b);
                        }

                    }

                    r.Close();
    //关闭下载文件 
                    Response.End();//结束文件下载 
                }

                Response.Clear();
                
    if (forceDownload)
                
    {
                    Response.AppendHeader(
    "Content-Disposition""attachment; filename=" + fileName);
                }

                
    else
                
    {
                    Response.AppendHeader(
    "Content-Disposition""filename=" + fileName);
                }


                
    using (System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(picDic + fileName)))
                
    {
                    
    if (image.RawFormat.Equals(ImageFormat.Bmp))
                    
    {
                        Response.ContentType 
    = "image/bmp";
                    }

                    
    else
                    
    {
                        
    if (image.RawFormat.Equals(ImageFormat.Gif))
                        
    {
                            Response.ContentType 
    = "image/gif";
                        }

                        
    else
                        
    {
                            
    if (image.RawFormat.Equals(ImageFormat.Jpeg))
                            
    {
                                Response.ContentType 
    = "image/jpeg";
                            }

                            
    else
                            
    {
                                
    if (image.RawFormat.Equals(ImageFormat.Png))
                                
    {
                                    Response.ContentType 
    = "image/png";
                                }

                                
    else
                                
    {
                                    Response.ContentType 
    = "application/octet-stream";
                                }

                            }

                        }


                        image.Save(Response.OutputStream, image.RawFormat);
                    }

                }

            }
  • 相关阅读:
    UIButton的遍历
    UIbutton 圆角和边线
    UIbutton 和UIview 切单角
    为运行Microsoft Dynamics CRM 异步处理服务指定账户没有性能计数器权限
    转:细说ASP.NET Windows身份认证
    CRM 模拟用户
    CRM 2016 Get IOrganizationService
    模拟SQL用户 EXECUTE AS USER
    Calling Custom Actions from JavaScript
    processjs Documentation
  • 原文地址:https://www.cnblogs.com/weichuo/p/1206620.html
Copyright © 2011-2022 走看看