Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用异步进程上载多个图像_C#_Jquery_Ajax_Asp.net Mvc_Multithreading - Fatal编程技术网

C# 使用异步进程上载多个图像

C# 使用异步进程上载多个图像,c#,jquery,ajax,asp.net-mvc,multithreading,C#,Jquery,Ajax,Asp.net Mvc,Multithreading,我正在尝试根据ajax请求上传图像。在行动中,我必须保存4个不同大小的图像 我只想在当前线程中保存一个,在另一个线程中保存其余3个图像,这样用户就不必等待上传所有图像,因为我只需要第一个图像的路径 这就是我到目前为止所做的 [HttpPost] public JsonResult UploadImage(bool isUploadLogo = false) { HttpPostedFileBase file = Request.Files["UploadedImage"];

我正在尝试根据ajax请求上传图像。在行动中,我必须保存4个不同大小的图像

我只想在当前线程中保存一个,在另一个线程中保存其余3个图像,这样用户就不必等待上传所有图像,因为我只需要第一个图像的路径

这就是我到目前为止所做的

[HttpPost]
public JsonResult UploadImage(bool isUploadLogo = false)
{
        HttpPostedFileBase file = Request.Files["UploadedImage"];
        if (file != null)
        {
           var getExt = Path.GetExtension(file.FileName).ToLower();
           var getBytesArray = ResizeImage.ConvertToByte(file);
            string fileName = "TestImage" + DateTime.Now.Ticks.ToString() + getExt;
            var smallSizeImageBytes = ResizeImage.ResizeImageBytes(200, 200, getBytesArray, getImageFormat);
            var pic = ImageStore.UploadImage(fileName, file.ContentType, smallSizeImageBytes);

            //Save Slider Image,  Without Wait
            if (!isUploadLogo)
            {
                new Thread(() =>
                {
                    UploadSliderImage(getBytesArray);
                }).Start();
            }
            var result = new
            {
                url = "Folder Path Here" + fileName,        
                isSuccess = "true"
            };
            return Json(result, "text/plain", JsonRequestBehavior.AllowGet);
        }
}

public void UploadSliderImage(byte[] CroppedImage)
{
    //Code for upload diffrent size images
}
虽然我在另一个线程中调用了UploadSliderImage,但在客户端,直到UploadSliderImage过程完成,我才得到结果


如何将数据返回到客户端而不等待上载剩余图像。

我认为如果您使用的是.net 4.0及更高版本,最好使用Task而不是Thread类。下面是一个示例代码,您可以基于它。它将立即返回Json而不完成任务

        Debug.WriteLine("Image Upload");

        Task tsk = new Task(() =>
        {
            Thread.Sleep(5000); //This is your UploadSlider Image
            Debug.WriteLine("Done");

        });
        tsk.Start();

        return Json("result", JsonRequestBehavior.AllowGet);

它不影响所用的总时间。我正在浏览器控制台中检查它。早些时候大约需要800毫秒,现在仍然需要同样的时间。这等于不使用
任务上传图像的总时间
是否尝试过调试?Return Json将在Debug.WriteLine(“Done”)之前首先调用;对你说得对
return
语句在
Debug.Writeline
之前被调用。但我不知道为什么要花同样的时间。这就回答了你的问题。我不确定时间部分,但我认为这解决了您的问题(将数据返回到客户端,而无需等待上载剩余图像)