使用FileStream和FileUpload添加图像

时间:2017-07-12 20:49:46

标签: c# filestream

我正在尝试将图像文件上传到Server.MapPath(“〜/ Pictures”)。我相信FileUpload已经有效了。 FileStream未将选定的图像放入“Pictures /".

        if (FileUpload1.HasFile)
        {
            string imagename = System.IO.Path.GetFileName(FileUpload1.FileName);
            string ext = System.IO.Path.GetExtension(FileUpload1.FileName);
            string imagefile = Server.MapPath("Pictures/" + imagename + ext);
            byte[] Image = null;
            if (ext == ".jpg" | ext == ".gif" | ext == ".png" | ext == ".bmp")
            {
                Image = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile UploadedImage = FileUpload1.PostedFile;
                UploadedImage.InputStream.Read(Image, 0, (int)FileUpload1.PostedFile.ContentLength);
                int numBytesToRead = Image.Length;
                try
                {
                    using (FileStream fsNew = new FileStream(imagefile, FileMode.Create, FileAccess.Write))
                    {
                        fsNew.Write(Image, 0, numBytesToRead);
                    }
                }
                catch (FileNotFoundException ioEx)
                {
                    Label100.Text = "Filestream Add Pictures: " + ioEx.ToString();
                }
            }

0 个答案:

没有答案