ASMX文件上传

发布于 2024-10-14 14:02:28 字数 111 浏览 3 评论 0原文

有没有办法使用 ASMX Web 服务将文件从本地文件系统上传到服务器中的文件夹(没有 WCF,不要问为什么:)?

UPD

PS文件大小可以是 2-10 GB

Is there a way to upload a file from local filesystem to a folder in a server using ASMX web services(no WCF, don't ask why:)?

UPD

P.S.file size can be 2-10 GB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-10-21 14:02:28

当然:

[WebMethod]
public void Upload(byte[] contents, string filename)
{
    var appData = Server.MapPath("~/App_Data");
    var file = Path.Combine(appData, Path.GetFileName(filename));
    File.WriteAllBytes(file, contents);
}

然后公开服务,从 WSDL 生成客户端代理,调用标准内容。

--

更新:

我现在看到您关于处理大文件的更新。 WCF 中内置的带流的 MTOM 协议针对处理此类问题进行了优化场景。

Sure:

[WebMethod]
public void Upload(byte[] contents, string filename)
{
    var appData = Server.MapPath("~/App_Data");
    var file = Path.Combine(appData, Path.GetFileName(filename));
    File.WriteAllBytes(file, contents);
}

then expose the service, generate a client proxy from the WSDL, invoke, standard stuff.

--

UPDATE:

I see your update now about handling large files. The MTOM protocol with streaming which is built into WCF is optimized for handling such scenarios.

旧人 2024-10-21 14:02:28

在开发将大文件上传到服务器的免费工具时,我还使用 .NET 2.0 和 Web 服务。

为了使应用程序对非常大的文件具有更强的容错能力,我决定不上传一个大的 byte[] 数组,而是进行“分块”上传。

例如,为了上传 1 MB 文件,我确实调用了上传 SOAP 函数 20 次,每次调用都会传递一个 50 KB 的 byte[] 数组,并再次在服务器上将其连接在一起。

我也会数包裹,当一个包裹掉下来时,我会多次尝试重新上传。

这使得上传的容错能力更强,并且 UI 的响应速度更快。

如果您有兴趣,这是该工具的CP文章

When developing my free tool to upload large files to a server, I am also using .NET 2.0 and web services.

To make the application more error tolerant for very large files, I decided to not upload one large byte[] array but instead do a "chuncked" upload.

I.e. for uploading a 1 MB file, I do call my upload SOAP function 20 times, each call passing a byte[] array of 50 KB and concating it on the server together again.

I also count the packages, when one drops, I try to upload it again for several times.

This makes the upload more error tolerant and more responsive in the UI.

If you are interested, this is a CP article of the tool.

椒妓 2024-10-21 14:02:28

对于非常大的文件,将它们发送到 Web 服务的唯一有效方法是使用 MTOM 。并且 MTOM 仅在 WCF 中受支持,您已排除了这一点。使用旧式 .asmx Web 服务执行此操作的唯一方法是 @Darin Dimitrov 给出的答案。使用该解决方案,您将不得不承受文件进行 Base64 编码的成本(带宽增加 33%)。

For very large files, the only efficient way to send them to web services is with MTOM. And MTOM is only supported in WCF, which you have ruled out. The only way to do this with old-style .asmx web services is the answer that @Darin Dimitrov gave. And with that solution, you'll have to suffer the cost of the file being base64 encoded (33% more bandwidth).

醉梦枕江山 2024-10-21 14:02:28

我们有相同的要求,基本上是使用客户端的标准 FileUpload 控件通过 HTTP POST 上传文件。
最后,我们只是向 ASMX Web 服务项目添加了一个 ASPX 页面(毕竟它只是一个 Web 项目)——这允许我们上传到 ie http://foo/bar/Upload.aspx当 Web 服务位于 http://foo/bar/baz.asmx 时。这保留了 Web 服务中的功能,即使它使用单独的网页。

这可能符合也可能不符合您的要求,@Darins 方法也可以作为一种解决方法,但您必须为此在客户端进行修改,这对我们来说不是一个选择。

We had the same requirement, basically uploading a file via HTTP POST using the standard FileUpload controls on the client side.
In the end we just added an ASPX page to the ASMX web service project (after all its just a web project) - this allowed us to upload to i.e. http://foo/bar/Upload.aspx when the web service was at http://foo/bar/baz.asmx. This kept the functionality within the web service, even though it was using a separate web page.

This might or might not fit your requirements, @Darins approach would work as a workaround as well but you would have to make modifications on the client side for that, which wasn't an option for us.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文