先简单说明下我的代码:
private static string path;--先设置一个静态变量 用于接受路径插入数据库的
 private void Upload()
    {
        Boolean fileOK = false;
        path = Server.MapPath("~/UploadedImages/");
        if (FileUpload1.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            string[] allowedExtensions ={ ".gif", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }            }
        }
        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
                Label1.Text = "文件上传成功";
            }
            catch (Exception ex)
            {
                Label1.Text = "错误信息:" + ex.Message.ToString();
            }
        }
        else
        {
            Label1.Text = "文件格式有误,无法上传";
        }
    }然后写下我的CLICK事件:
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int value = Convert.ToInt32(dropDownDepartment.SelectedValue);
        Upload();
        path = path + FileUpload1.FileName;
        Response.Write(path);        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        SqlCommand cmd = new SqlCommand("insert into News (Did,Newsname,Time,Author,ClickTime,NewsDetails,NewsPictureUrl) values(@Did,@Newsname,@Time,@Author,@ClickTime,@NewsDetails,@NewsPictureUrl)", cn);
        cmd.Parameters.Add("@Did", SqlDbType.Int).Value = value;
        cmd.Parameters.Add("@Newsname", SqlDbType.VarChar).Value = txtNewsname.Text;
        cmd.Parameters.Add("@Time", SqlDbType.VarChar).Value = txtTime.Text;
        cmd.Parameters.Add("@Author", SqlDbType.VarChar).Value = txtAuthor.Text;
        cmd.Parameters.Add("@ClickTime", SqlDbType.Int).Value = 0;
        cmd.Parameters.Add("@NewsDetails", SqlDbType.VarChar).Value = txtNewsDetails.Text;
        cmd.Parameters.Add("@NewsPictureUrl", SqlDbType.VarChar).Value = path;
        cn.Open();
        int a = cmd.ExecuteNonQuery();
        cn.Close();
        if (a > 0)
        {
            Response.Redirect("NewsSelect.aspx");
        }
        else
        {
            Label1.Text = "新闻没有添加成功,请重新添加";
        }
        
        
    }现在我说下我的问题:
我在本地上传后(假设文件名为1.JPG) 得到的结果是绝对路径插入数据库后的:f:\shanfei\UploadedImages\1.jpg但是当我把项目FTP上传后,所有上传的图片都是一个X,无法显示。我想来想去都是路径的问题。请有经验的大大帮我仔细看下。

解决方案 »

  1.   

    FileUpload1.PostedFile.SaveAs(path + "/"+FileUpload1.FileName); 
      

  2.   

    protected void btnUpdaload_Click(object sender, EventArgs e)
        {
            if (imageUpload.HasFile)
            {
                string extension = Path.GetExtension(imageUpload.FileName);
                if (SafeTool.CheckAllowFile(extension))
                {
                    string path = Server.MapPath("~/Images/FlashImages");
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                    imageUpload.PostedFile.SaveAs(path + "\\" + fileName);                Uri uri = Request.Url;
                    //注意,这里数据库里保存的是相对地址,显示给用户的是绝对URL,这样即使域名或者路径发生相对更改,也不会影响使用
                    //txtLinkUrl.Text = string.Format("{0}://{1}/download/{2}", uri.Scheme, uri.Authority, fileName);//显示给用户的地址
                    //hfPath.Value = string.Format("download/{0}", fileName);//数据库中保存的地址
                    txtImagePath.Text = string.Format("Images/FlashImages/{0}", fileName);
                }
                else
                {
                    JScript.Alert("你上传的文件格式不符合要求!");
                }
            }
            else
            {
                JScript.Alert("请选择一个文件上传!");
            }
        }
      

  3.   


    能不能再贴下 你的CLICK事件
      

  4.   

    string filepath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "/UserFiles/" + uploadType;
    File1.PostedFile.SaveAs(filepath + "/" + filename);
    这是我的代码,请楼主注意
    System.Web.HttpContext.Current.Request.PhysicalApplicationPath
    它是可以使你的路径变成相对的
      

  5.   

    像这种web程序,一般来说对于上传文件其路径都是虚拟路径在数据库中存储的