I have a scenario of a single 'Save' button upload either text or image at a time. So, when I am trying to save text, it is working fine, posted & giving response but when I am trying to save image then it is posted fine but not receiving response. It is receiving empty result/response.
Please help me...My code is like below
$.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;
}
I am attaching network images also. If anyone can tell me where is the actual problem.