不使用ASP.NET中的伺服器控制項將如何上傳檔案?

來源:互聯網
上載者:User

標籤:style   http   java   使用   os   io   檔案   ar   

遇到檔案的上傳時,可能會有大部分的開發人員喜歡使用伺服器控制項,雖然很方便,但是卻不能很好的控制,不具靈活性。

現給出例子,使用html標籤語言靈活的控制檔案的上傳。

1、html部分

<input type="file" id="uploadFile" name="uploadFile" style="display: none;"/><a href="#" id="uploads">上傳檔案</a><input type="hidden" id="txtFile" name="txtFile" value=""/><div class="img"></div>

2、JS部分

<script type="text/javascript">        $(document).ready(function () {            //單擊div觸發檔案的上傳事件            $(".img").click(function() {                $("#uploadFile").click();            });            $("#uploads").click(function () {                ajaxFileUpload();            });        });                function ajaxFileUpload() {            $.ajaxFileUpload            (                          {                    url: ‘/plus/upload.aspx‘, //用於檔案上傳的伺服器端請求地址                        secureuri: false, //是否需要安全性通訊協定,一般設定為false                    fileElementId: ‘uploadFile‘, //檔案上傳域的ID                    dataType: ‘json‘, //傳回值類型 一般設定為json                    success: function(data, status) //伺服器成功響應處理函數                    {                        $("#img1").attr("src", data.imgurl);                        $("#txtFile").val(data.imgurl);                        if (typeof (data.error) != ‘undefined‘) {                            if (data.error != ‘‘) {                                alert(data.error);                            } else {                                alert(data.msg);                            }                        }                    },                    error: function(data, status, e) //伺服器響應失敗處理函數                    {                        alert(e);                    }                }            );            return false;        }    </script>

3、uoload.aspx.cs代碼部分

protected void Page_Load(object sender, EventArgs e){                        HttpFileCollection files = Request.Files;            string msg = string.Empty;            string error = string.Empty;            string imgurl = string.Empty;                        if (files.Count > 0)            {                string fileUrl = Server.MapPath("/") + "uploadfiles\\scholar\\temp\\";  //擷取web伺服器所在的實體路徑                //若檔案夾不存在就建立                if (!Directory.Exists(fileUrl))                {                    Directory.CreateDirectory(fileUrl);                }                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + files[0].FileName.Substring(files[0].FileName.IndexOf(‘.‘));//修改檔案名稱                files[0].SaveAs(fileUrl + fileName);    //將上傳的檔案儲存到指定伺服器的檔案夾中                msg = "上傳成功! 檔案大小為:" + ((float)files[0].ContentLength/(1024*1024)).ToString("0.00") + "MB";                imgurl = "/uploadfiles/scholar/temp/" + fileName;                string res = "{ error:‘" + error + "‘, msg:‘" + msg + "‘,imgurl:‘" + imgurl + "‘}";                Response.Write(res);                Response.End();                           }}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.