asp.net 檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   for   

public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            //context.Response.Write(context.Request["cheshi1"]);            if (context.Request.Files.Count > 0)            {                for (int i = 0; i < context.Request.Files.Count; i++)                {                    HttpPostedFile file = context.Request.Files[i];                                        //檢查檔案類型,只接收jpg,jpeg,bmp格式                    if (file.ContentType=="image/jpeg")                    {                        //通過字串雜湊碼,建立多層目錄隨機儲存路徑。                        string guid = Guid.NewGuid().ToString();                        int ram = guid.GetHashCode();                        int dirA = ram & 0xf;  //0xf ‘1111‘ 整數15                        int dirB = (ram >> 4) & 0xf;  //ram 右移4位                        int dirC = (ram >> 8) & 0xf;  //ram 右移8位                        string path = context.Server.MapPath(string.Format("Upload/{0}/{1}/{2}/", dirA.ToString(), dirB.ToString(), dirC.ToString()));                        System.IO.Directory.CreateDirectory(path); //建立目錄。                        file.SaveAs(path + guid + "_" + file.FileName);                        //建立縮圖並儲存                        Image image = Image.FromStream(file.InputStream);                        Image smallImg = new Bitmap(200, 200 * image.Height / image.Width);                        Graphics g = Graphics.FromImage(smallImg);                        g.DrawImage(image, 0, 0, smallImg.Width, smallImg.Height);                        smallImg.Save(path + guid + "_small" + file.FileName);                        context.Response.Write("“" + file.FileName + "”上傳成功!\r\n");                    }                    else                    {                        context.Response.Write("上傳檔案格式錯誤! is:" + file.FileName + "\r\n");                    }                }                            }            else            {                context.Response.Write("上傳失敗!");            }        }
    <script type="text/javascript">        window.onload = function () {            document.getElementById(‘add‘).onclick = function () {                var file1 = document.getElementById(‘file1‘);                var ext = /\.[^\.]+$/.exec(file1.value.toLowerCase());                if (ext == ‘.jpeg‘ || ext == ‘.jpg‘ || ext == ‘.bmp‘) {                    return;                }                else {                    alert(‘上傳檔案格式錯誤!‘);                    return false;                }            }        };    </script>
<form id="form1" method="post" action="AddNews.aspx" enctype="application/x-www-form-urlencoded"><input type="file" name="txtImage" id="file1" value="" /><input id="add" type="submit" value="提交" />

 

asp.net 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.