我如何在客户端知道 HTTP 处理程序何时完成处理?
对于一个简单的解决方案来说可能是一个很长的问题,但这里是...
我有一个定制的 silverlight 控件,用于选择多个文件并将它们发送到服务器。 它使用 WebCLient 控件的 OpenWriteAsync 方法将文件发送到通用处理程序 (FileReciever.ashx)。
基本上,silverlight 代码对每个文件执行类似的操作:
WebClient client = new WebClient();
client.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
client.OpenWriteAsync(handlerUri);
服务器端处理程序只是读取传入流,然后对生成的字节数组进行更多处理。
问题是,一旦所有数据都通过网络发送,客户端 OpenWriteCompleted 就会完成。 然后我的代码将继续下一个文件。 我真正想要的是等到 ASHX 处理程序完成该请求的所有处理。 我怎么做? WebClient 有等待机制吗? 我可以在处理程序中的 HttpContext 上执行任何回调吗? 我应该使用其他类型的传输技术吗? 请指教!
Probably a long question for a simple solution, but here goes...
I have a custom made silverlight control for selecting multiple files and sending them to the server. It sends files to a general handler (FileReciever.ashx) using the OpenWriteAsync method of a WebCLient control.
Basically, the silverlight code does something like this for each file:
WebClient client = new WebClient();
client.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
client.OpenWriteAsync(handlerUri);
The server side handler simply reads the incoming stream, and then does some more processing with the resulting byte array.
THE PROBLEM is that client side OpenWriteCompleted is done as soon as all the data has been sent over the wire. My code will then contine with the next file. What I really want is to wait until the ASHX handler has finished with all it's processing of that request. How do I do that? Any wait mechanism on WebClient? Any callback I can do on the HttpContext in the handler? Should I use some other kind of transfer technique? Please advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,也许一个简单的解决方案可能是用 GUID 标记 url(每个文件或传输的 guid 是唯一的,无论对您的情况有意义)。 然后,您可以拥有另一个简单的 Web 服务,该服务能够根据 guid 检查其他服务的状态,并让您的 silverlight 客户端查询该新服务的处理状态(通过向新的 Web 服务传递该服务的 guid)过去的转移)。
Hrm, maybe a simple solutioin could be to tag the url with a GUID(the guid being unique per file, or transfer, whatever makes sense to your situatuation). Then you can have another simple web service that is capable of checking on the status of the other service, based on the guid, and have your silverlight client query that new service for its processing status(by passing the new web service the guid of the past transfer).
我假设您担心从处理程序返回的数据需要很长时间才能传输,并且在此期间服务器没有被使用。 没有办法告诉服务器何时完成处理,因此我认为在不更改架构的情况下无法做到这一点。
我会让你的处理程序只有某种标识符(例如 GUID 或 int),可用于在另一个请求中检索处理程序的结果。 因此,页面将调用处理程序,处理程序将存储结果并返回标识符,页面将第二次调用处理程序并调用另一个处理程序以获取第一次调用的结果。 这将使您的服务器在数据传输时保持使用状态。
I'm assuming that you're concerned that the data being returned from the handler is taking a long time to transfer and the server is not being utilized during that time. There isn't a way to tell when the server is done processing, so I don't think you can do this without changing your architecture.
I would have your handler only an identifier of some sort (like a GUID or int) that can be used to retrieve the result of the handler in another request. So the page would call the handler, the handler would store the result and return the identifier, the page would call the handler the second time and call another handler to get the result of the first call. This would keep your server in use while your data was transferring.
或者你可以使用 JavaScript (jQuery) 来完成它......如果你不介意使用 JavaScript 的话。
Or you can probably do it with JavaScript (jQuery)... if you don't mind using JavaScript that is.
如果文件不是很大,并且可以将每个文件保留在内存中,则一个丑陋但有效的解决方案是将它们转换为字符串并使用 UploadStringAsync 方法发送它们。
如果文件大小不受限制,请避免使用此方法,但如果您现在可以看到它们相对较小,则可以使用此方法。
If files are not very big, and is feasible to keep each of them in memory, an ugly yet effective solution is converting them to strings and sending them using the UploadStringAsync method.
Avoid this approach if file size is unbounded, but if you can now that they will be relatively small, it is possible to use this approach.
Silverlight 论坛中也提出了同样的问题。 Microsoft 认可的答案是,您不能使用 WebClient 和 OpenWriteAsync 来做到这一点。 您需要使用 UploadStringAsync 或 HttpWebRequest。
The same question has been asked in Silverlight forums. The Microsoft endorsed answer was that you can't do that with WebClient and OpenWriteAsync. You need to either user UploadStringAsync or an HttpWebRequest.