0
    HttpFileCollection oHttpFileCollection = e.PostedFiles;
    HttpPostedFile oHttpPostedFile = null;
    if (e.HasFiles)
    {
        for (int n = 0; n < e.Count; n++)
        {
            oHttpPostedFile = oHttpFileCollection[n];
            if (oHttpPostedFile.ContentLength <= 0)
                continue;
            else
                oHttpPostedFile.SaveAs(Server.MapPath("Files") + "\\" + System.IO.Path.GetFileName(oHttpPostedFile.FileName));
    }

如何转换HttpFileCollection为字节并读取流,然后在 asp.net 中将图像显示到图像控件

谢谢

4

1 回答 1

3
HttpFileCollection oHttpFileCollection = e.PostedFiles;
HttpPostedFile oHttpPostedFile = null;
if (e.HasFiles)
{
    for (int n = 0; n < e.Count; n++)
    {
        oHttpPostedFile = oHttpFileCollection[n];
        if (oHttpPostedFile.ContentLength <= 0)
        {
            continue;
        }
        else
        {
            var filename = Path.GetFileName(oHttpPostedFile.FileName);
            var path = Path.Combine(Server.MapPath("~/Files/"), filename);
            oHttpPostedFile.SaveAs(path);

            // Now you could display each image in a dynamically added Image
            // control to the page:
            Image image = new Image();
            image.ImageUrl = "~/Files/" + filename;

            // I assume that you have a reference to the current page
            // so that you could append image controls to it.
            // You could also append the images to a placeholder or a Panel
            // on your WebForm
            this.Controls.Add(image);
        }
}
于 2013-02-04T10:04:52.590 回答