阅读背景:

知道图片路径,怎样获取图片并上传

来源:互联网 
C:\Users\Public\Pictures\SamplePictures
C:\Users\Public\Pictures\SamplePictures\1.jpg
如果我只有这个文件名,怎么把这图片上传到服务器呢?
大家帮帮忙啊,至于为什么要这么做就不用问了哈哈!

4 个解决方案

#1


try
        {
            string name = FileUpload1.FileName;
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = name.Substring(name.LastIndexOf(".") + 1).ToLower();

            if (type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg")
            {
                try
                {
                    ImgUploads iu = new ImgUploads(DropDownList1.SelectedItem.Text);
                    String id=iu.GetMaxId();//获取数据库最大ID号用来命名图片
                    String ReName = id + "." + type;//图片重命名

                    String Ipath = Server.MapPath("~/hy/upimg") + "\\" + ReName;//文件实际路径
                    FileUpload1.SaveAs(Ipath);//上传到图片目录

                    String Tpath = Server.MapPath("~/hy/thumbar") + "\\" + ReName;//缩略图路径
                    PictureSlightly.MakeImage(Ipath,Tpath,100,130,"hw");//生成缩略图

//上传到数据库
                    iu.UpImg(Convert.ToInt32(id),DropDownList1.SelectedItem.Text,ReName.ToString());

                    Label1.Text = "上传成功,图片大小:" + size + "B";
                    this.thumbar.Src = "~/hy/thumbar/" + ReName;
                }
                catch(Exception ex)
                {
                    Page.RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!"+ex.Message.ToString()+"');</script>");
                }
                
            }
            else
            {
                Page.RegisterStartupScript("error", "<script>alert('图片格式不对!');</script>");
            }
        }
        catch
        {
            Page.RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!');</script>");
        }
    } 

#2


在Asp.Net中一般可以使用两种方式来操作,一是如楼上发的代码,直接使用服务器端FileUpload控件,然后如上操作来把你的图片上传到服务器上;二是可以使用HttpPostFile类来操作,此时需要注意一点是需要使用form的Post方式来提交数据,可以使用html file控件

#3


通过Ftpwebrequest或web services上传

#4


无控件上传,各位有详细点的办法吗?

.jpg C:\Users\Public\Pictures\SamplePictures
C:\Users\Public\Pictures\SamplePictures\1.jpg
如果我只有这个文件名,怎么把这图片上传到服务器呢?
大家帮帮忙啊,至于为什么要这么做就不用问了哈哈!

4 个解决方案

#1


try
        {
            string name = FileUpload1.FileName;
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type = name.Substring(name.LastIndexOf(".") + 1).ToLower();

            if (type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg")
            {
                try
                {
                    ImgUploads iu = new ImgUploads(DropDownList1.SelectedItem.Text);
                    String id=iu.GetMaxId();//获取数据库最大ID号用来命名图片
                    String ReName = id + "." + type;//图片重命名

                    String Ipath = Server.MapPath("~/hy/upimg") + "\\" + ReName;//文件实际路径
                    FileUpload1.SaveAs(Ipath);//上传到图片目录

                    String Tpath = Server.MapPath("~/hy/thumbar") + "\\" + ReName;//缩略图路径
                    PictureSlightly.MakeImage(Ipath,Tpath,100,130,"hw");//生成缩略图

//上传到数据库
                    iu.UpImg(Convert.ToInt32(id),DropDownList1.SelectedItem.Text,ReName.ToString());

                    Label1.Text = "上传成功,图片大小:" + size + "B";
                    this.thumbar.Src = "~/hy/thumbar/" + ReName;
                }
                catch(Exception ex)
                {
                    Page.RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!"+ex.Message.ToString()+"');</script>");
                }
                
            }
            else
            {
                Page.RegisterStartupScript("error", "<script>alert('图片格式不对!');</script>");
            }
        }
        catch
        {
            Page.RegisterStartupScript("error", "<script>alert('图片上传失败,请重试!');</script>");
        }
    } 

#2


在Asp.Net中一般可以使用两种方式来操作,一是如楼上发的代码,直接使用服务器端FileUpload控件,然后如上操作来把你的图片上传到服务器上;二是可以使用HttpPostFile类来操作,此时需要注意一点是需要使用form的Post方式来提交数据,可以使用html file控件

#3


通过Ftpwebrequest或web services上传

#4


无控件上传,各位有详细点的办法吗?

.jpg



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: