我正在使用 ASP.NET MVC 开发一个视频网站。
我希望在我的应用程序中拥有的一项功能是转码视频。但由于转码过程可能非常耗时,我想向客户端用户展示该过程的进度。
因此,我的架构是使用一个控制器操作来处理整个转码过程,并将其进度写入存储在服务器上的文件中。同时,在转码过程中,我使用Ajax调用另一个控制器动作来读取指定文件,检索进度信息并将其发送回客户端显示,每2秒一次。
为了实现我的计划,我编写了以下代码:
服务器端:
public class VideoController : Controller
{
//Other action methods
....
//Action method for transcoding a video given by its id
[HttpPost]
public async Task<ActionResult> Transcode(int vid=0)
{
VideoModel VideoModel = new VideoModel();
Video video = VideoModel.GetVideo(vid);
string src = Server.MapPath("~/videos/")+video.Path;
string trg = Server.MapPath("~/videos/") + +video.Id+".mp4";
//The file that stores the progress information
string logPath = Server.MapPath("~/videos/") + "transcode.txt";
string pathHeader=Server.MapPath("../");
if (await VideoModel.ConvertVideo(src.Trim(), trg.Trim(), logPath))
{
return Json(new { result = "" });
}
else
{
return Json(new { result = "Transcoding failed, please try again." });
}
}
//Action method for retrieving the progress value from the specified log file
public ActionResult GetProgress()
{
string logPath = Server.MapPath("~/videos/") + "transcode.txt";
//Retrive the progress from the specified log file.
...
return Json(new { progress = progress });
}
}
客户端:
var progressTimer = null;
var TranscodeProgress = null;
// The function that requests server for handling the transcoding process
function Transcode(vid) {
// Calls the Transcode action in VideoController
var htmlobj = $.ajax({
url: "/Video/Transcode",
type: "POST",
//dataType: 'JSON',
data: { 'vid': vid },
success: function(data)
{
if(data.result!="")
alert(data.result);
}
else
{
//finalization works
....
}
}
});
//Wait for 1 seconds to start retrieving transcoding progress
progressTimer=setTimeout(function ()
{
//Display progress bar
...
//Set up the procedure of retrieving progress every 2 seconds
TranscodeProgress = setInterval(Transcoding, 2000);
}, 1000);
}
//The function that requests the server for retrieving the progress information every 2 seconds.
function Transcoding()
{
//Calls the GetProgress action in VideoController
$.ajax({
url: "/Video/GetProgress",
type: "POST",
//dataType: 'JSON',
success: function (data)
{
if (data.progress == undefined || data.progress == null)
return;
progressPerc = parseFloat(data.progress);
//Update progress bar
...
}
});
}
现在是客户端代码和Transcode
动作方法一切正常。问题是GetProgress
方法永远不会被调用,直到Transcode
动作完成了它的整个过程。那么我的代码有什么问题吗?我该如何修改它,使这两个动作自发地发挥作用,从而实现我的目标?
Update
根据Alex的回答,我发现我的问题是由Asp.Net框架的会话锁定机制引起的。所以禁用SessionState
of my VideoController
或者将其设置为只读确实会使控制器在执行转码视频的操作方法时响应检索转码进度的请求。但因为我用Session
in my VideoController
要存储一些变量以便在多个请求中使用,这种方式不能成为解决我的问题的合适解决方案。有没有更好的方法来解决呢?