2017-06-16 16 views
0

我發現了很多用於網頁文本編輯的解決方案,但最方便和最輕量級的文本編輯器之一是NiEditor。我正在燒着腦袋,通過編輯器將圖像上傳到我自己的服務器。默認編輯器在[IMGUR]服務器上傳圖像。 我的問題是如何上傳圖像到我自己的服務器而不是IMGUR服務器? Image Link如何將NicEdit富文本編輯器與asp.net C#(.aspx)集成在一起

+0

NicEdit文檔頁面將是一個很好的地方,可以在提問之前開始閱讀......所以,請看[NicEdit wiki](http://wiki.nicedit.com/w/page/515)/Configuration%20Options) – Nino

+0

我在提問之前先通過NicEdit wiki。關於NiceEdit的配置有很好的描述,但所有描述都只關於PHP。我在asp.net中搜索了一天的解決方案,但沒有找到解決方案。 – HEARTBEAT

回答

1

在這裏我發現了很好的解決方案,在.net中上傳圖片。通用處理程序是圖像上傳的最佳選擇。 請按照以下步驟使用C#將NicEditor與asp.net集成。

  1. 從nicedit.com下載最新的nicEdit.js。

  2. 修改線沒有1888與下面的代碼

nicURI: "images.ashx" 
  • 創建的通用處理程序上傳命名images.ashx圖像。
  • 將下面的代碼寫入public HandlerRequest(HttpContext上下文)中的handler.ashx文件中。

    string baseImageLocation = HttpContext.Current.Server.MapPath("~/Admin/imgs/"); 
    
    HttpPostedFile Files; 
    Files = context.Request.Files[0]; // Load File collection into HttpFileCollection variable. 
    //Files.ContentLength; 
    //Files.ContentType; 
    if (Files != null && Files.ContentLength > 0) 
    { 
        System.IO.Stream fileStream = Files.InputStream; 
        fileStream.Position = 0; 
    
        byte[] fileContents = new byte[Files.ContentLength]; 
        fileStream.Read(fileContents, 0, Files.ContentLength); 
        string fileExt = System.IO.Path.GetExtension(Files.FileName).ToLower(); 
        string fileName = Path.GetFileName(Files.FileName); 
        System.Drawing.Image image = null; 
        if (fileName != null) 
        { 
         if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".jpg" || fileExt == ".png" || fileExt == ".jpeg") 
         { 
          image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(fileContents)); 
          if (System.IO.File.Exists(baseImageLocation + "/" + fileName)) 
           fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt; 
          Files.SaveAs(baseImageLocation + fileName); 
         } 
        } 
        string link = VirtualPathUtility.ToAbsolute("~/Admin/imgs/") + fileName; 
    
        string imageHeight = image.Height.ToString(); 
        string imageWidth = image.Width.ToString(); 
        string json = ""; 
    
    
        json += "{" + 
          "\"links\": \"" + link + "\"," + 
          "\"width\": \"" + imageWidth + "\"," + 
          "\"height\": \"" + imageHeight + "\"" + 
          "}"; 
    
    
        context.Response.ContentType = "application/json"; 
        context.Response.Write(json); 
    
    } 
    
  • 請注意,將您nicEdit.js文件和文件yourhandler.ashx在同一個文件夾中,以便它可以很容易地訪問處理程序的路徑。

  • 在解決方案目錄中創建要上傳圖像(由編輯器上傳)的圖像文件夾。
  • +0

    嗨。我用你的代碼在本地服務器上保存圖像。它工作正常。但我無法將上傳的圖像顯示到textarea中。你有想法嗎?你能幫我嗎? – gkrishy