首页 新闻 搜索 专区 学院

asp.net 下载文件如何使文件下载到指定的文件夹中

0
悬赏园豆:5 [已解决问题] 解决于 2012-06-12 17:05

asp.net 下载文件如何使文件下载到指定的文件夹中

study_hard_01的主页 study_hard_01 | 初学一级 | 园豆:126
提问于:2012-04-05 09:19
< >
分享
最佳答案
0

asp.net C#

.aspx主要代码

<TR>
  
  
      <Th><%=reqTitle %> No.:</th>
  <td>&nbsp;&nbsp;<asp:TextBox runat="server" Css />
        (*)</TD>
 </TR>
 <TR>
  
      <Th>File:</th>
  <td>&nbsp;&nbsp;<input runat="server" type="file" >(*)</TD>
 </TR>
 <TR>
  <TD colspan=2 align=middle><asp:Button runat="server" Text="" OnClick="btnUpload_Click" Css onMouseDown="this.style.backgroundImage='url(../Sales/images/Uploada3.gif)'"
onMouseOver="this.style.backgroundImage='url(../Sales/images/Uploada2.gif)'"  
onMouseOut="this.style.backgroundImage='url(../Sales/images/Uploada1.gif)'"/>
  <input type="button" name="close" value="" onMouseDown="this.style.backgroundImage='url(../Sales/images/close3.gif)'"
onMouseOver="this.style.backgroundImage='url(../Sales/images/close2.gif)'"  
onMouseOut="this.style.backgroundImage='url(../Sales/images/close1.gif)'" onclick="javascript:self.close();"/></TD>


 </TR>

 

.cs主要代码

 

if (File1.PostedFile.ContentLength != 0)
                {
                    fileName = FileUtility.UploadFile(File1, Server.MapPath("~/PFImage/"),DateTime.Now.ToString("yyyyMMddHHmmddfff")+
                        System.IO.Path.GetFileName(File1.PostedFile.FileName));
                    if (fileName == "")
                    {
                        //上传失败                         

                      return;
                    }
                    else
                    {
                             //上传成功                               

                                                                                                            
                    }
                }

文件上传类主要代码

命名空间

using System.IO;
using System.Net;

 #region 上传文件
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="inputFile">System.Web.UI.HtmlControls.HtmlInputFile</param>
        /// <param name="path">文件存储目录,物理文件路径</param>
        /// <returns></returns>
        public static string UploadFile(System.Web.UI.HtmlControls.HtmlInputFile inputFile, string path,string namedFileName)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                inputFile.PostedFile.SaveAs(path + namedFileName);
            }
            catch (Exception)
            {
                namedFileName = "";
            }

            return namedFileName;
        }
        #endregion

收获园豆:3
a85295853 | 初学一级 |园豆:2 | 2012-04-09 16:55
其他回答(1)
0

用SaveFileDialog对象。

private void SaveButton_Click(object sender, EventArgs e) { 

SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = @"C:\"; saveFileDialog1.Title = "Save text Files"; 

saveFileDialog1.CheckFileExists = true; 

saveFileDialog1.CheckPathExists = true; 

saveFileDialog1.DefaultExt = "txt";

 saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

 saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true;

 if (saveFileDialog1.ShowDialog() == DialogResult.OK) 

{ textBox1.Text = saveFileDialog1.FileName; } 

}

收获园豆:2
悟行 | 园豆:12422 (专家六级) | 2012-04-05 09:49

他说的是 网页版的 

支持(0) 反对(0) 二十二号同学 | 园豆:790 (小虾三级) | 2012-04-05 11:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册