.net的上传和下载

上传核心代码:

foreach (System.Web.UI.WebControls.FileUpload HIF in fileList)
            {
                try
                {


                    string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);

                    HIF.PostedFile.SaveAs(baseLocation + fn);//这是最重要的步骤
                    fileUploaded++;
                    
                    file.filename = fn;
                    file.filesize = HIF.PostedFile.ContentLength;
                    file.replyid = new ProjectManage.BLL.reply().GetMaxId()+1;
                    file.path += GetFileName(file);

                    fileBLL.Add(file);
                    this.ReplyDataBind();
                }
                catch (Exception err)
                {
                    lbMsg.Text = "上传错误 " + baseLocation
                     + "<br>" + err.ToString();
                }
            }



下载核心代码:

        string baseLocation = Server.MapPath("UploadFiles/"); ;
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(baseLocation + filename);
        if (fileInfo.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.FullName, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fileInfo.FullName);
            Response.End();
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "", "<script language='javascript'>alert('文件不存在!');</script>");
        }

你可能感兴趣的:(C++,c,UI,.net,C#)