HTML頁面部分代碼
<img alt="老師相片128*128" width="128px" height="128px" src="../img/ImgTeacher.jpg" id="ImgTeacher" /> <br /> <input type="file" id="fulFile" name="fulFile" accept="image/gif,image/jpeg,image/png" class="file" value="上傳圖片" />一般處理程序的代碼
public void PRocessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string msg = string.Empty; string error = string.Empty; string result = string.Empty; string filePath = string.Empty; string fileNewName = string.Empty; HttpFileCollection files = context.Request.Files; // HttpPostedFile files = context.Request.Files["fulFile"]; if (files.Count > 0) { //設置文件名 fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName); //保存文件 files[0].SaveAs(context.Server.MapPath("~/UploadFile_TeacherImg/" + fileNewName)); msg = "文件上傳成功!"; result = "{msg:'" + msg + "',filenewname:'" + fileNewName + "'}"; } else { error = "文件上傳失?。?; result = "{ error:'" + error + "'}"; } context.Response.Write(result); context.Response.End(); }總結: 這里其實是轉載別人的代碼然后自己拿來用的,直接拿來用,一直出一些bug: 1.Ajax進不去一般處理程序——改動了ashx文件,他有兩個文件(.ashx.cs || .ashx)必須用記事本單獨打開.ashx(
<%@ WebHandler Language="C#" CodeBehind="UploadAjaxImg_Teacher.ashx.cs" Class="PianoManagement.BaseDataManagement.UploadAjaxImg_Teacher" %>//第一行的Class要和你現在的一般處理程序的Class相對應!用記事本改掉2.AJAX成功了,但是一個拿不到File的值,這里要檢查一下前臺的File空間有沒有加Name的屬性。 3.File的控件不可以被賦值?。ò踩珕栴}?。?/p>
新聞熱點
疑難解答