文件的-------上传,下载,删除.

原创 2007年09月21日 12:42:00

 #region 下载查看文件方法
    public bool downAndlook(string fileserverURL)
    {
        try
        {
            string fileserverpath = Page.Server.MapPath(fileserverURL);
            System.IO.FileInfo fi = new System.IO.FileInfo(fileserverpath);
            fi.Attributes = System.IO.FileAttributes.Normal;
            System.IO.FileStream filestream = new System.IO.FileStream(fileserverpath, System.IO.FileMode.Open);
            long filesize = filestream.Length;
            int i = Convert.ToInt32(filesize);

            Page.Response.ContentType = "application/octet-stream";
            Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileserverpath, System.Text.Encoding.UTF8));
            Page.Response.AddHeader("Content-Length", filesize.ToString());
            byte[] fileBuffer = new byte[i];
            filestream.Read(fileBuffer, 0, i);
            filestream.Close();
            Page.Response.BinaryWrite(fileBuffer);
            Page.Response.End();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.GetType().ToString() + "');</script>");
            return false;
        }
    }
    #endregion

  #region 删除文件方法
    public bool DeleteFile(string filename)
    {
        string filePath = Server.MapPath(".") + "//" + filename;
        System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
        fi.Attributes = System.IO.FileAttributes.Normal;
        if (System.IO.File.Exists(filePath) == false)
        {
            return false;
        }
        else
        {
            System.IO.File.Delete(filePath);
            return true;
        }
    }
    #endregion

    #region 上传方法
    public bool stringbind(string filestringPath)
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        /// '状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        System.Text.StringBuilder strMsg2 = new System.Text.StringBuilder();
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字
                HttpPostedFile postedFile = files[iFile];
                if (postedFile.ContentLength > 41943040)//判断上传文件的大小,前提是已经在Web.Config中设置了最大上传文件的大小为
                {                                       //< 40M 设置内容这:“<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>”
                    Response.Write("<script>alert('文件过大,上传失败!');</script>");
                }
                else
                {
                    if (postedFile.ContentLength > 0 && (postedFile.FileName.ToString() != ""))
                    {
                        string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("//"));
                        // 判断上传的文件是否存在.....
                        if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == true)
                        {
                            string filesName = System.IO.Path.GetFileName(postedFile.FileName);
                            //如果上传的文件存在,则不上传,并且显示上传失败的文件名.
                            string lbl2 = strMsg2.Append(filesName + ";").ToString();
                            Response.Write("<script>alert('你上载的文件中'" + lbl2 + "'已经存在,请更改此文件名称后,重新上传!');</script>");
                        }
                        else
                        {
                            if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == false)
                            {
                                string filesName = System.IO.Path.GetFileName(postedFile.FileName);
                                System.IO.Directory.CreateDirectory(Page.Server.MapPath(".") + "//" + filestringPath);
                                postedFile.SaveAs(Page.Server.MapPath(".") + "//" + filestringPath + fileName);
                                string FIleServerPath = Page.Server.MapPath(".") + "//" + filestringPath + fileName;//上传到服务器中的/路径
                                string FileNoServerPath = filestringPath + fileName;
                                string lbl = strMsg.Append(filesName + ";").ToString();

                                #region 插入企业闲置商标附件
                                NetEFO.FunctionLib f = new NetEFO.FunctionLib();
                                oXZSBFiles_Fid = " ";
                                int FBase_XZSBFilesTypeID = 1;
                                string sqlFileString = "insert Bill_XZSBFiles values(" + oXZSBFiles_Fid + "," + Fid + "," + FBase_XZSBFilesTypeID +
                                                       ",'" + filesName + "','" + FIleServerPath + "','" + FileNoServerPath + "')";
                                DBO.Insert(CnnString, sqlFileString);
                                #endregion
                            }

                        }
                    }
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion 

另外在Web.Config中加入  <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/> 能够设置上传文件的大小为 40M.

相关文章推荐

ASP.NET 文件上传

  • 2012年09月28日 13:22
  • 24KB
  • 下载

基于ASP.NET文件上传设计与实现

  • 2007年09月14日 19:29
  • 128KB
  • 下载

Java利用SMB上传、下载、读取(内容或文件名)、复制、删除远程文件

package com.yls; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; imp...

FTP连接,上传,下载,删除文件方法

解决的两个问题 1.设置连接超时时间,如下: ftp.setConnectTimeout(3*1000);//设置连接ftp超时时间3秒2.文件下载后,出现文件中中文乱码的情况,解决办法是,设置编...

ftpclient 文件上传、下载、删除

  • 2014年08月04日 10:36
  • 241KB
  • 下载

JAVA实现FTP服务器文件的上传,下载,删除功能

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; impo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章: 文件的-------上传,下载,删除.
举报原因:
原因补充:

(最多只允许输入30个字)