创建一个文件夹并上传图片到ASP.NET该文件夹的最佳方式? [英] Best way to create a folder and upload a image to that folder in ASP.NET?

查看:175
本文介绍了创建一个文件夹并上传图片到ASP.NET该文件夹的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何,我必须去创建一个文件夹说,在我的根图片,然后上传在文件上传控件的图像转换为图片任何code examples文件夹?
如果你不想给我所有的code,我会很乐意用一个漂亮的链接还向我展示如何将VB.NET(C#也行)来完成。

Any code examples on how i must go about creating a folder say "pics" in my root and then upload a images from the file upload control in into that "pics" folder? If you don't want to give me all the code, i will be happy with a nice link also to show me how this will be done in VB.NET (C# is also ok).

推荐答案

的try / catch是你:)

Try/Catch is on you :)

public void EnsureDirectoriesExist()
        {

                // if the \pix directory doesn't exist - create it. 
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/pix/")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/pix/"));
                }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {


                if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".jpg")
                {
                    // create posted file
                    // make sure we have a place for the file in the directory structure
                    EnsureDirectoriesExist();
                    String filePath = Server.MapPath(@"~/pix/" + FileUpload1.FileName);
                    FileUpload1.SaveAs(filePath);


                }
                else
                {
                    lblMessage.Text = "Not a jpg file";
                }


        }

这篇关于创建一个文件夹并上传图片到ASP.NET该文件夹的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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