当用户输入URL和Path of Image时,如何在asp.net中使用验证控件?

时间:2016-09-13 15:14:00

标签: asp.net

我的asp.net网页包含两个文本框,一个用于URL,另一个用于ImagePath 我想要用户以下列格式输入的URL 例如,URL文本框中的http://articles.site.com。如果用户输入的网址不是http://articles.site.com,则不应该使用此格式。消息应显示为输入正确的URL。 对于用户ImagePath文本框输入的ImagePath,如下所示。 例如http://articles.site.com/ImageServer/Public/2016/July/EnglishBanner/effective-skunk-rinse-recipe.jpg

2 个答案:

答案 0 :(得分:0)

protected void Upload(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}

在ASP.Net网页的Page Load事件中,文件从Images文件夹中获取,然后获取的图像文件绑定到ASP.Net GridView控件

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
     List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            string fileName = Path.GetFileName(filePath);
            files.Add(new ListItem(fileName, "~/Images/" + fileName));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
}

答案 1 :(得分:0)

您已将此功能用于验证网址。

    private bool IsUrlValid(string url)
{

    string pattern = @"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$";
    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
    return reg.IsMatch(url);
}