$ .ajax帖子在服务器上不起作用,但在localhost

时间:2016-10-07 13:28:15

标签: jquery ajax post asynchronous jquery-post

我有一个单一的' Save'按钮一次上传文本或图像。所以,当我试图保存文本时,它工作正常,发布和&给予回应,但当我试图保存图像时,它被发布正常但没有收到回复。它正在接收空的结果/响应。

请帮帮我......我的代码如下所示

 $.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

1 个答案:

答案 0 :(得分:0)

感谢大家。

Action方法中的图像保存功能出现问题...我用以下&amp ;;替换了图像保存逻辑。它现在正在运作。

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);
            }