如何发送和接收文件?
我想要以下类型的功能
有两个用户A
和B
都正在访问同一页面
此页面有一个按钮(发送文件)。
当A
单击按钮时,应出现打开对话框
框
当A
从打开对话框中选择文件< /code> 框,那么
B
的页面上应该会出现一个包含文件名的链接
当 B
单击链接时,SaveAs
> 应出现对话框,给出名称和路径后,应开始下载文件。
在这方面的任何帮助将不胜感激!
I want following type of functionality
There are two users A
and B
both are visiting same page
This page has a button(Send File) .
When A
clicks on button an Open Dialog
box should appear
When A
selects a file from Open Dialog
box then there should be a link appear on B
’s page containing file name
When B
clicks on link, a SaveAs
dialog should appear and after giving name and path file download should start.
Any kind of help in this context will be appreciated !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打开对话框是通过 html 输入实现的:
另存为对话框是通过 html 锚点实现的:
用户 A 选择文件后,您可以使用 AJAX 或表单 onsubmit 通过您创建的 upload.aspx 页面上传文件。
在用户 B 的计算机上,您使用 AJAX 调用 updatefilelist.aspx 页面,该页面返回可用文件列表。一旦用户 A 的新文件上传并保存在服务器上,用户 B 的 AJAX 调用对 updatefilelist.aspx 的下一次 ping 将更新其屏幕上显示的列表以包含新文件。
单击文件会调用具有所需文件名的 download.aspx 页面 - 您需要设置 Response.ContentType 和 Response.AddHeader("Content-Disposition", "attachment:filename="" " & IO.Path.GetFileName(Request.QueryString("filename")) & """") 此处。然后将自动弹出另存为对话框。
The open dialog is acheived with the html input:
<input type="file">
The save as dialog is acheived with the html anchor:
<a href="www.myserver.com/download.aspx?filename='the file'">
After user A selects a file, you either use AJAX or the form onsubmit to upload the file via an upload.aspx page that you create.
On user B's machine, you use AJAX to call an updatefilelist.aspx page which returns the list of files available. Once user A's new file has been uploaded and saved on the server, the next ping from user B's AJAX call to updatefilelist.aspx will update the list shown on his screen to include the new file.
Clicking on the file calls the download.aspx page with the desired filename - you need to set
Response.ContentType
andResponse.AddHeader("Content-Disposition", "attachment:filename=""" & IO.Path.GetFileName(Request.QueryString("filename")) & """")
here. The save as dialog will then automatically pop up.该页面可以分为两半:一半是文件上传控件,另一半通过 AJAX 每 3-4 秒轮询一次以查看是否有新文件。
“A”面有一个标准文件上传控件,并使用标准 .NET 方式保存上传的文件(请参阅 FileUpload.PostedFile.SaveAs(path),简单的东西)。文件上传完成后,刷新此页面。
“B”面是一个 div,它从 AJAX 调用、XML、JSON 或纯文本获取其内容。有一个名为“link.aspx”的 AJAX 页面,可以以您想要的任何格式返回内容。
对于链接,如果您想强制“下载”窗口,那么文件需要是浏览器中无法正常显示的类型,或者您需要使用另一个 aspx 页面来提供文件并强制保存或打开对话框。
该 aspx 页面将被称为“file.aspx”之类的内容,您可以传入一个查询字符串参数(“id”或其他内容)来告诉它您想要什么文件。此页面后面的代码将从该查询字符串参数中找出您需要什么文件,然后提供该文件:
Well the page could be split into two halves: one with the file uploading controls and the other that polls every 3-4 seconds via AJAX to see if there's new files.
Side "A" has a standard file upload control and uses the standard .NET way of saving uploaded files (see FileUpload.PostedFile.SaveAs(path), easy stuff). After the file upload is done, refresh this page.
Side "B" is a div that gets its contents from an AJAX call, XML, JSON or just plain ol' text. Have an AJAX page called "link.aspx" that kicks back the content in whatever format you want.
For the links, if you want to force the "download" window, then either the files need to be a type that isn't displayed normally in the browser, or you need to use another aspx page to serve out the file and force a dialog to Save or Open.
That aspx page will be called something like "file.aspx" and you could pass in a querystring param ("id" or something) to tell it what file you want. Your code behind of this page will figure out what file you need from that querystring param, then will serve out the file: