用于上载和下载的ASP.NET代码 [英] ASP.NET code for Upload and Download

查看:77
本文介绍了用于上载和下载的ASP.NET代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/女士,

我已经创建了一个网站.我可以在网站上选择上载和下载.
我想知道用于上传和下载的ASP.NET代码.我已经从工具箱中控制了上传,因此我可以从浏览窗口中选择所需的文件.但要将其上传到默认路径,我需要代码.
请帮我解决这个问题.

谢谢.

Dear Sir/Mam,
Hi,
i have created a website. I have option for Upload and download in the site.
I want to know the ASP.NET code for upload and download. i have taken upload control from the toolbox so i can select the desired file from browae window. but to upload it to a default path i need code.
plz help me out on this.

Thank you.

推荐答案

用于文件上传

for file upload

if (FileUploadControl.HasFile)
              {
                  try
                  {
                      filename = Path.GetFileName(FileUploadControl.FileName);
                      FileUploadControl.SaveAs(Server.MapPath("~/Documents/LetterInward/") + filename);
                      objInwardLetter.strPath = "~/Documents/LetterInward/" + filename;
                      StatusLabel.Text = "Upload status: File uploaded!";






用于下载文件








FOR DOWNLOADING FILE





protected void lnkAttachment_Click(object sender, EventArgs e)
   {
       string filename = string.Empty;
       string path = "~/Documents/LetterInward/" + lblcurentFilePath.Text;
       if (lblcurentFilePath.Text != "")
       {
           if (path != "")
           {
               filename = (path).Remove(0, (path.LastIndexOf('/') + 1));
               System.IO.FileInfo file;
               file = new System.IO.FileInfo(Server.MapPath(path));
               Response.Clear();
               Response.ClearContent();
               Response.ClearHeaders();
               //Added later on 22-Nov-2011 because zip file not extracted at client side
               Response.Buffer = true;
               Response.AppendHeader("Content-Length", file.Length.ToString());
               Response.ContentType = GetMimeTypeByFileName(filename);
               Response.AppendHeader("Content-Disposition:", "attachment; filename=" + filename);
               Response.TransmitFile(file.FullName);
               Response.Flush();
               ApplicationInstance.CompleteRequest();
           }
       }
   }
   public string GetMimeTypeByFileName(string sFileName)
   {
       string sMime = "application/octet-stream";

       string sExtension = System.IO.Path.GetExtension(sFileName);
       if (!string.IsNullOrEmpty(sExtension))
       {
           sExtension = sExtension.Replace(".", "");
           sExtension = sExtension.ToLower();

           if (sExtension == "xls" || sExtension == "xlsx")
           {
               sMime = "application/ms-excel";
           }
           else if (sExtension == "doc" || sExtension == "docx")
           {
               sMime = "application/msword";
           }
           else if (sExtension == "ppt" || sExtension == "pptx")
           {
               sMime = "application/ms-powerpoint";
           }
           else if (sExtension == "rtf")
           {
               sMime = "application/rtf";
           }
           else if (sExtension == "zip")
           {
               sMime = "application/zip";
           }
           else if (sExtension == "mp3")
           {
               sMime = "audio/mpeg";
           }
           else if (sExtension == "bmp")
           {
               sMime = "image/bmp";
           }
           else if (sExtension == "gif")
           {
               sMime = "image/gif";
           }
           else if (sExtension == "jpg" || sExtension == "jpeg")
           {
               sMime = "image/jpeg";
           }
           else if (sExtension == "png")
           {
               sMime = "image/png";
           }
           else if (sExtension == "tiff" || sExtension == "tif")
           {
               sMime = "image/tiff";
           }
           else if (sExtension == "txt")
           {
               sMime = "text/plain";
           }
       }
       return sMime;
   }


嘿,

保存使用
FileUpload1.SaveAs(Server.MapPath(〜/App_Data/")+ FileUpload1.FileName);
祝你好运
Hey,

to Save use
FileUpload1.SaveAs(Server.MapPath("~/App_Data/")+FileUpload1.FileName);
best Luck


亲爱的朋友,

这些链接将对您有所帮助:-

要下载
http://www.daniweb.com/web-development/aspnet/threads/252778 [ ^ ]

C#图片下载 [ http://www.codeproject.com/Articles/1757/File-Upload-with- ASP-NET [ ^ ]

http://www.codeproject.com/Articles/1757/File-Upload-with- ASP-NET [ ^ ]

http://forums.asp.net/t/1241497.aspx/1 [ ^ ]

ASP.NET-使用JavaScript和C# [ ^ ]

使用C#,JavaScript和ASP上传图像.NET 2.0客户端回调 [
Dear friend,

These links will be helpful:-

For Download
http://www.daniweb.com/web-development/aspnet/threads/252778[^]

C# Image Download[^]

For Upload
http://www.codeproject.com/Articles/1757/File-Upload-with-ASP-NET[^]

http://www.codeproject.com/Articles/1757/File-Upload-with-ASP-NET[^]

http://forums.asp.net/t/1241497.aspx/1[^]

ASP.NET - Upload File With Loading Image using JavaScript and C#[^]

Upload Images Using C#, JavaScript and ASP.NET 2.0 Client Callbacks[^]

Thanks


这篇关于用于上载和下载的ASP.NET代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆