C# WebServices 和 Flex RPC 会话不起作用
看起来我面临一个问题。我有 .NET + Flex 应用程序。
我在 Flex 中有一个组件,可以将 .XLS 文件上传到服务器。 该组件有一个进度条,它使用 fileref.addEventListener(ProgressEvent.PROGRESS, ProgressHandler); 更新其进度条和标签;
private function progressHandler(event:ProgressEvent):void
{
uploadProgress.label = "Uploading File %3%%";
uploadProgress.setProgress(event.bytesLoaded, event.bytesTotal);
}
我使用了 FileReference 和所有内容来完成这个。我用这样的东西将发布的文件保存在服务器中
HttpFileCollection uploadedFiles = Request.Files;
string path = string.Empty;
string sFile = Request.Params["Name"];
if (uploadedFiles.Count != 0)
{
HttpPostedFile userPostedFile = uploadedFiles[0];
if (userPostedFile.ContentLength > 0)
{
path = Server.MapPath(".\\Uploads\\") + sFile;
userPostedFile.SaveAs(path);
}
}
这工作得很好,我的栏和标签刷新正常,我的文件上传到临时文件夹中,一切正常,所以回到 Flex 我可以在文件是通过 DataEvent.UPLOAD_COMPLETE_DATA
上传的。 因此,当调度此事件时,我调用 My WebService 的 WebMethod 来读取已发布的文件并迭代所有记录。该文件可能有 65,000 多行。因此,信息会在 Oracle 数据库中的某些表中逐行更新。
我想要归档的是当文件发布到服务器时具有相同的效果。我想要一个进度条并显示上传到 Oracle DB 的行的百分比。
所以我在 Flex 中所做的是这样的:
//Service is the WebService Instance
ShowPopUpProgress();
Service.wsUploadFileToDb(FileName);
Service.wsUpdatePercentage();
protected function wsUpdatePercentage_result(event:ResultEvent):void
{
compProgress.uploadProgress.label = "Uploading to DB %3%%";
compProgress.uploadProgress.setProgress(event.result.Actual, event.result.Total);
if(event.result.Actual != event.result.Total)
{
Service.wsUpdatePercentage();
}
else
{
RemovePopUpProgress();
}
}
在 C# 中我这样做了,
[WebMethod(EnableSession = true)]
public void wsUploadFileToDb(string fileName)
{
HttpContext.Current.Session["Total"] = 0;
HttpContext.Current.Session["Actual"] = 0;
//... And in the Loop
HttpContext.Current.Session["Total"] = myDataTable.Rows.Count;
for (int i = 0; i < myDataTable.Rows.Count; i++)
{
HttpContext.Current.Session["Actual"] = i + 1;
}
}
[WebMethod(EnableSession = true)]
[XmlInclude(typeof(Percentage ))]
public Percentage wsUpdatePercentage()
{
Percentage oPercentage= new Percentage ();
if (HttpContext.Current.Session["Total"] != null)
{
oPercentage.Total = Convert.ToInt32(HttpContext.Current.Session["Total"]);
oPercentage.Actual= Convert.ToInt32(HttpContext.Current.Session["Actual"]);
}
}
我不知道此时您是否明白什么我想做。我不知道这是否是最好的方法,或者还有其他方法可以实现我的目标。但这就是我到目前为止所拥有的。由于某种原因,这不起作用,会话变量正确存储在 wsUploadFileToDb 方法,但我在 wsUpdatePercentage 中看不到它们,它们只是不“共享”会话。两种方法我都调试过,会话ID不一样。所以会话没有被共享。我不知道还能做什么。我在网站中的第一个页面是 aspx.. 我在会话中放置了一个虚拟变量,因为我在一篇文章中读到您必须这样做,才能在网络服务中启动会话,但仍然不走运。
任何帮助将不胜感激。
提前致谢。
look im facing a problem. I have .NET + Flex application.
I have a component in Flex who uploads a .XLS file to the server.
This component has a Progress Bar
and it updates its bar and the Label with the fileref.addEventListener(ProgressEvent.PROGRESS, progressHandler);
private function progressHandler(event:ProgressEvent):void
{
uploadProgress.label = "Uploading File %3%%";
uploadProgress.setProgress(event.bytesLoaded, event.bytesTotal);
}
I used the FileReference and all the things to acomplish this. I save the posted file in the server with something like this
HttpFileCollection uploadedFiles = Request.Files;
string path = string.Empty;
string sFile = Request.Params["Name"];
if (uploadedFiles.Count != 0)
{
HttpPostedFile userPostedFile = uploadedFiles[0];
if (userPostedFile.ContentLength > 0)
{
path = Server.MapPath(".\\Uploads\\") + sFile;
userPostedFile.SaveAs(path);
}
}
This works perfectly fine, my bar and my label are refreshed ok, and my file is uploaded in a Temp folder and everything is ok, so going back to Flex I can listen when the file was uploaded through the DataEvent.UPLOAD_COMPLETE_DATA
.
So When this event is dispatched I call a WebMethod of My WebService to Read that posted File and iterate over all the records. The file could have 65,000+ rows. So the info is updated row by row in a Oracle DB in some tables.
What I'm trying to archieve is to have the same effect when the File is posted to the server. I want to have a progress Bar and show the % of rows uploaded to the Oracle DB.
So what I Did in Flex was something like this:
//Service is the WebService Instance
ShowPopUpProgress();
Service.wsUploadFileToDb(FileName);
Service.wsUpdatePercentage();
protected function wsUpdatePercentage_result(event:ResultEvent):void
{
compProgress.uploadProgress.label = "Uploading to DB %3%%";
compProgress.uploadProgress.setProgress(event.result.Actual, event.result.Total);
if(event.result.Actual != event.result.Total)
{
Service.wsUpdatePercentage();
}
else
{
RemovePopUpProgress();
}
}
And in C# I did this
[WebMethod(EnableSession = true)]
public void wsUploadFileToDb(string fileName)
{
HttpContext.Current.Session["Total"] = 0;
HttpContext.Current.Session["Actual"] = 0;
//... And in the Loop
HttpContext.Current.Session["Total"] = myDataTable.Rows.Count;
for (int i = 0; i < myDataTable.Rows.Count; i++)
{
HttpContext.Current.Session["Actual"] = i + 1;
}
}
[WebMethod(EnableSession = true)]
[XmlInclude(typeof(Percentage ))]
public Percentage wsUpdatePercentage()
{
Percentage oPercentage= new Percentage ();
if (HttpContext.Current.Session["Total"] != null)
{
oPercentage.Total = Convert.ToInt32(HttpContext.Current.Session["Total"]);
oPercentage.Actual= Convert.ToInt32(HttpContext.Current.Session["Actual"]);
}
}
I dont know if at This point you understand what I want to do. I dont know if this is the best way, or there is another way to archieve my goal. But thats what I have so Far. And for some reason this is not working, The Session variables are stored correctly in the
wsUploadFileToDb method but i Can't see them in the wsUpdatePercentage, they just dont "share" the session. I debugged both methods and the session ID is different. So The session is not being shared. I dont know what else to do. My First page in the Site is an aspx.. I put a dummy variable in session, because i read in one post that you had to do it, to start the session in the webservices but still not luck.
Any help would be really appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以建议一种替代方法;如果您维护一个单独的表来保存有关数据上传到 Oracle DB 的进度信息,而不是尝试在 Web 应用程序和服务之间共享会话信息,则会更容易。
基本上,您将为从 Excel 插入表中的每一行更新此表,并且您可以从服务中读取此进度表中的信息,并将其报告给 Flex 客户端。
这样,您还可以获得从 Excel 文件到数据库的所有上传的审核日志,以及有关上传是否正确以及导入的行数的信息。
I can suggest an alternative approach; instead of trying to share the session information between the web app and the service it would be easier if you maintain a separate table which holds the information about the progress of data upload to the Oracle DB.
Basically you will be updating this table for every row that is inserted into the table from the excel, and from the service you can read the information from this progress table to report it to the flex client.
This way, you also have an audit log of all the uploads you have done from excel files to the database and information about if they succeeded properly and the count of rows that were imported.