2016-10-07 26 views
1

私は一度に1つのテキストまたは画像をアップロードする単一の[保存]ボタンのシナリオを持っています。だから、私はテキストを保存しようとしているとき、それは正常に動作している投稿&が応答を与えるが、私は画像を保存しようとしているとき、それはうまく投稿されますが、応答を受信して​​いません。空の結果/応答を受信して​​います。

私はまた、ネットワークの画像を添付しています

$.ajax({ 
    async: false, 
    url: localStorage.getItem('environment') + 'QuibStream/AddQuib', 
    type: 'POST', 
    dataType: 'text', 
    data: { body: body, time: seconds, isSeedQuib: IsSeedQuib, seedQuibType: SeedQuibType, parentId: SelectedQuibId, movieId: queryStringValuefromKey("movieId"), IsScreenshot: IsScreenshot }, 
    success: function (response) { 
     if (response != undefined && response != null && response.length > 0) { 
      var SeedquibClass = ""; 
      quibContent = JSON.parse(response); 
     } 
    } 
}); 
[Authorize] 
[HttpPost] 
public ActionResult AddQuib(string body, int time, bool isSeedQuib, string SeedQuibType, int parentId = 0, string movieId = "", bool IsScreenshot = false) 
{ 
    QuibStream quib = new QuibStream(); 
    QuibStream objQuib = new QuibStream(); 

    try 
    { 
     //quib.MovieId = Convert.ToInt32(Session["MovieId"]); 
     if (movieId.Length > 0) 
      quib.MovieId = Convert.ToInt32(movieId); 
     else 
      quib.MovieId = (Request.Params["MovieId"] != null && Convert.ToString(Request.Params["MovieId"]).Trim().Length > 0) ? Convert.ToInt32(Convert.ToString(Request.Params["MovieId"]).Trim()) : 0; 
     quib.UserId = Convert.ToInt32(cookie["UserId"]); 

     // this replaces new line also with single space 
     //quib.Body = Regex.Replace(body.Trim(), @"\s+", " "); 

     if (!IsScreenshot) 
      quib.Body = body.Trim(); 
     else 
      quib.Body = body; 

     RegexOptions options = RegexOptions.None; 
     Regex regex = new Regex(@"[ ]{2,}", options); 
     if (!IsScreenshot) 
      quib.Body = regex.Replace(quib.Body, @" "); 

     quib.Time = time; 
     quib.IsQuibZero = time == 0 ? true : false; 
     quib.ParentId = parentId == 0 ? 0 : parentId; 

     quib.IsSeedQuib = isSeedQuib; 
     quib.SeedQuibType = quib.IsSeedQuib ? SeedQuibType : null; 
     quib.IsScreenshot = IsScreenshot; 

     if (IsScreenshot) 
     { 
      var fileType = quib.Body.Split('/')[1]; 
      fileType = fileType.Split(';')[0]; 
      Guid fileNameGuid = Guid.NewGuid(); 
      string ImageString = quib.Body.Split(',')[1]; 
      var newImageByte = Convert.FromBase64String(ImageString); 
      byte[] DocBytesArray = new byte[newImageByte.Length + 1]; 
      if (ImageString != null) 
       DocBytesArray = newImageByte; 
      //byte[] bytes = DocBytesArray; 
      var fs = new BinaryWriter(new FileStream(System.Web.HttpContext.Current.Server.MapPath("~\\Images\\Screenshots") + "\\" + fileNameGuid.ToString() + "." + fileType, FileMode.Append, FileAccess.Write)); 
      fs.Write(DocBytesArray); 
      fs.Close(); 
      quib.Body = @"/Images/Screenshots/" + fileNameGuid.ToString() + "." + fileType; 
     } 

     objQuib = _quibService.AddQuib(quib); 
    } 
    catch (Exception ex) 
    { 
     WinEventLog.eventLog.WriteEntry(string.Format("QuibStream 'AddQuib()' Failed. Error : '{0}'", ex.Message), EventLogEntryType.Error, 100); 
     return Json(null); 
    } 

    var jsonResult = Json(objQuib, JsonRequestBehavior.AllowGet); 
    jsonResult.MaxJsonLength = int.MaxValue; 
    return jsonResult; 
} 

以下のように私のコードがある...私を助けてください。誰かが実際の問題がどこにあるのか教えていただけたら。誰にでも

Networks when Body text

Networks when Body Image

+3

コンソールにエラーがありますか?また、 'async:false'を削除してください。それは恐ろしいです –

+1

あなたはデータ型を使用しています: "テキスト:"!あなたはどのようにデータ型の画像を送ることを期待していますか? – AthMav

+1

@AthMav 'dataType'はあなたが得るもので、あなたが送るものではありません。しかし、あなたのコメントは何らかの形で正しいです。これは明らかに問題 –

答えて

0

感謝。

Actionメソッドの画像保存機能に問題がありました...画像保存ロジックを次のように置き換えました。&現在動作しています。

if (IsScreenshot) 
      { 
       string fileType = ImageFormat.Png.ToString(); 
       string fileNameGuid = Guid.NewGuid().ToString(); 

       quib.Body = Convert.ToString("/Images/Screenshots/" + fileNameGuid + "." + fileType).Trim(); 

       // Convert Base64 String to byte[] 
       byte[] imageBytes = Convert.FromBase64String(body); 
       MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); 

       // Convert byte[] to Image 
       ms.Write(imageBytes, 0, imageBytes.Length); 
       System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); 
       string newFile = fileNameGuid + "." + fileType; 
       string filePath = Path.Combine(Server.MapPath("~/Images/Screenshots") + "\\", newFile); 
       image.Save(filePath, ImageFormat.Png); 
      } 
関連する問題