如何上传图片 [英] How do I upload an image

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

问题描述

尝试上传和图片。



Trying to Upload and Image.

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")
                {
                    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();
                    }
                }





我的尝试:



只是将文件名用于图片。无法正常工作



What I have tried:

Tried just using the filename into pictures. Did not work

推荐答案

检查您尝试将文件保存到的目录的权限。

运行网站的帐户默认情况下,它不会对文件系统具有写入权限,如果是这样,您就可以将任何类型的文件上传到网站。



亲切的问候
Check the permissions to the directory you are trying to save the file to.
The account running the web-site will not have write permissions to the file system by default, if it did you would be able to upload any type of file to the website.

Kind Regards


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

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