文件上传(1)

2009-2-23下午2:30到4:00

一直在查:百度,Google,博客园.

结果太多了,但是没有完全符合我的要求的答案.我想要的是将PDF文件上传到服务器的文件夹中,然后用户可以在客户端读取PDF文件.

首先解决上传的问题,然后是读取(下载??)

在MSDN查到Fileupload控件.希望是我所要的.晚上回家去试一试.

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        Boolean fileOK = false;//文件上传是否OK,设为否
        String path = Server.MapPath("~/UploadedImages/");//设定存储路径
        if (FileUpload1.HasFile)
        {
            String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions =
                {".gif", ".png", ".jpeg", ".jpg"};
          for (int i = 0; i < allowedExtensions.Length; i++)
          {
               if (fileExtension == allowedExtensions[i])
               {
                    fileOK = true;
               }
          }//将图片格式更改为PDF格式

if (fileExtension == @".xls") //@"pdf"??
            {
                fileOK = true;
            }
//以上摘自http://www.cnblogs.com/PLAYBOY840616/archive/2006/12/29/607020.html
        }

        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path
                    + FileUpload1.FileName);
                Label1.Text = "File uploaded!";
            }
            catch (Exception ex)
            {
                Label1.Text = "File could not be uploaded.";
            }
        }
        else
        {
            Label1.Text = "Cannot accept files of this type.";
        }
    }
}

你可能感兴趣的:(文件上传)