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

 #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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值