当前位置:编程学习 > C#/ASP.NET >>

ASP.net中上传图片怎么做,请各位高手赐教!本人感激不尽!我写代码了,但是用不起!


        String filename, savepath;
        String[] tempfilename;
        savepath = Server.MapPath("image/");
        FileUpload fileupload1 = (FileUpload)FormView1.FindControl("FileUpload1");
        filename = fileupload1.FileName;
        tempfilename = filename.Split('.');
        filename = tempfilename[tempfilename.Length-1];
        filename = System.Guid.NewGuid().ToString() + "." + filename;
        savepath = savepath + filename;
        if(fileupload1.HasFile==true)
        {
            fileupload1.SaveAs(savepath);
        }
        Image image1 = (Image)FormView1.FindControl("Image1");
        image1.ImageUrl = "~/image/"+filename;
        Session["filename"] = filename; --------------------编程问答-------------------- 这里是原文

/// <summary>
    /// 在服务器上新建目录 
    /// </summary>
    /// <param name="dir"></param>
    protected void createDir(string dir)
    {
        if (dir.Length == 0) return;
        if (!System.IO.Directory.Exists(Request.PhysicalApplicationPath + "\\" + dir))
            System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "\\" + dir);
    }

    /// <summary>
    /// 上传文件 如路径不存在则自动创建 Hackdv.cn
    /// </summary>
    /// <param name="FileUpload1"></param>
    /// <param name="txtUrl"></param>
    /// <param name="ChannelDir"></param>
    protected void setUploadFile(ref FileUpload FileUpload1, ref TextBox txtUrl, string ChannelDir)
    {
        if (FileUpload1.PostedFile != null)
        {
            string fileContentType = FileUpload1.PostedFile.ContentType;
            if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
            {
                if (FileUpload1.PostedFile.ContentLength > 1024 * 1024)
                {
                    Alert("上传文件大于1M,请使用FTP上传!");
                    return;
                }
                string filename = string.Empty;
                string filename2 = FileUpload1.PostedFile.FileName;
                filename2 = filename2.Substring(filename2.LastIndexOf('.'));
                char[] namechar = DateTime.Now.ToString().ToCharArray();
                for (int i = 0; i < namechar.Length; i++)
                {
                    if (namechar[i] != '-' && namechar[i] != ':' && namechar[i] != ' ')
                        filename += namechar[i].ToString();
                }
                filename += new Random().Next(100, 999).ToString();
                filename += filename2;
                string filepath = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
                filepath = ChannelDir + "/UserFiles/" + filepath;

                createDir(filepath);
                FileUpload1.PostedFile.SaveAs(Request.PhysicalApplicationPath + filepath + "/" + filename);
                filepath = filepath + "/" + filename;
                txtUrl.Text = filepath.ToString().Trim();
            }
            else
            {
                Alert("只能上传图片格式!");
            }
        }



比较简单的上传图片代码  有点过分简化了 看不明白就加群 15058618 --------------------编程问答-------------------- ···其中的Alert是个基类函数 ······ --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 这样的代码网上多的事,楼主自己找一下吧
一眼看到的错误是这个
tempfilename[tempfilename.Length-1];
tempfilename是一个数组,而你的[]内的参数是什么啊  --------------------编程问答-------------------- http://www.51aspx.com/Type/6/
看看里面有没有你需要的
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,