如何在asp.net上传图片 [英] how to upload images in asp.net

查看:59
本文介绍了如何在asp.net上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在asp.net上传图像

解决方案

在页面中添加FileUpload控件和按钮。给按钮文本上传(并给出两个明智的名字)

处理按钮的Click事件:



  ///   <  摘要 >  
/// 将文件上传到数据库。
/// < / summary >
/// < param < span class =code-summarycomment> name =sender > < / param >
/// < param name =e > < / param >
protected void butUpload_Click( object sender,EventArgs e)
{
StatusLabel.Text = SaveUpload(FileUpload1);
}
/// < 摘要 >
/// 将上传保存到数据库中。
/// < / summary >
/// < param < span class =code-summarycomment> name =fl > 包含控件下载。< / param >
/// < 返回 > 状态为字符串< / returns >
private string SaveUpload(FileUpload fl)
{
if (fl。 HasFile)
{
尝试
{
string filename = Path.GetFileName(fl.FileName);
byte [] filedata = fl.FileBytes;
...
return string .Format( {0}已上传,filename);
}
catch (例外情况)
{
return 无法上传文件。出现以下错误: + ex.Message;
}
}
return 请选择一个文件。;
}

在例程的中间,你可以将文件保存到磁盘,也可以保存到数据库,或者你需要做什么。


使用fileUpload控件。请参见此处 [ ^ ]。


如何以 [ ^ ]前两个结果来自Code Project。请参阅以下内容:



如何使用ASP.NET上传图像 [ ^ ]

.NET图像上传 [ ^ ]

i didn''t know how to upload images in asp.net

解决方案

Add an FileUpload control and a button to your page. Give the button the Text "Upload" (and give both sensible names)
Handle the Click event for the button:

/// <summary>
/// Upload the file to the database.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void butUpload_Click(object sender, EventArgs e)
    {
    StatusLabel.Text = SaveUpload(FileUpload1);
    }
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            ...
            return string.Format("{0} uploaded", filename);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }

In the middle of the routine, you can either save the file to disk, or to a database, or whatever you need to do with it.


use the fileUpload control. See here[^].


How bout starting with Google[^]? The first 2 results are from Code Project. See the following:

How to upload an image using ASP.NET[^]
.NET Image Uploading[^]


这篇关于如何在asp.net上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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