ASP. NET MVC files are uploaded across domains, receiving returned results

Source: Internet
Author: User

In the system we may have a variety of file uploads, so that we may be the file module to deploy a separate server, so that the upload will encounter cross-domain issues. We can upload to the server first and then through the httpclient and other technologies to upload to the file servers, so there is no cross-domain problem, but this is more than a step in the server relay, and now we say how the client directly across the domain upload to the file server.


1. File server deployment of a file upload interface (Implementation technology: WEBAPI,WEBSERVICE,MVC, etc.)

The file Upload interface example is as follows (MVC mode):

<summary>
Upload interface
</summary>
<param name= "Uploadpath" > Save file virtual path </param>
<returns></returns>
Public ActionResult Upload (string uploadpath)
{
var result = string. Empty;
Get uploaded file collection, support multi-file upload
var files = request.files;
To turn a virtual path into a physical file path
var FilePath = Server.MapPath (Uploadpath);
Determines whether the physical path exists, does not exist, and creates
if (! Directory.Exists (Uploadpath)) directory.createdirectory (FilePath);
foreach (HttpPostedFileBase item in files)
{
Combine the physical path and file name into a full path
FilePath = Path.Combine (FilePath, item. FileName);
Saving files to disk
Item. SaveAs (FilePath);
Combine file addresses that a browser can access
Result + = Path.Combine ("http://", "File server address", Uploadpath, item.) FileName) + ",";
}
Return results to Client
Return Redirect ("The client receives the page path of the returned results? result=" + (Result). Length > 0? Result. Substring (0, result. Length-1): ""));
}

2. The client needs to have a transit page that receives the results from the file server (a static HTML page that must be deployed with the system under the same domain name)

2.1 Examples of transit pages are as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> File Upload Results Transit page </title>
<script type= "Text/javascript" >
Set the domain name to the current system domain name, the upload interface needs to be set up so that the upload interface to get the results of the Transfer interface processing
Document.domain = "Current system domain name address";
Get the parameters in the address
Function GetParameter (val) {
The URI format may be:/HTTP//current system domain address/Receive Upload results Transit page address? result=http://file server address/upload path/filename 1,http://file server address/upload path/file name 2,http:// File server address/upload path/file name 3
var uri = Window.location.search;
var re = new RegExp ("" + val + "= ([^&?] *) "," IG ");
Return ((Uri.match (re))? (Uri.match (re) [0].substr (Val.length + 1)): null);
}
var upload_callback = function () {
var data = GetParameter ("s");
var location_hash = Location.hash;
if (Location_hash! = NULL && Location_hash! = "" && location_hash) {
var data = Location.hash? Location.hash.substring (1): ";
document.getElementsByTagName ("Body") [0].innerhtml = ' <pre> ' + unescape (data) + ' </pre> ';
} else {
var data = GetParameter ("s");
document.getElementsByTagName ("Body") [0].innerhtml = ' <pre> ' + unescape (data) + ' </pre> ';
}
};
</script>
<body onload= "Upload_callback (); >
</body>

2.2 Get the results of the transit page in the upload page

The sample code for the 2.2.1 upload page is as follows:

--upload interface to get the results of the transit page

<script type= "Text/javascript" >
Document.domain = "This must be set to the same domain as the relay page";
function Iframeload () {
var pre = document.getElementById ("Uploadfiles"). ContentWindow.document.getElementsByTagName ("Pre");
if (Pre.length > 0) {
alert (pre[0].innerhtml); This is the uploaded file address.
}
}
</script>

--use IFRAME to implement no-refresh uploads

<form method= "POST" action= "File Server interface Address" enctype= "Multipart/form-data" target= "Uploadfiles" >
<input type= "File" name= "Imgfile"/>
</form>

<iframe style= "Display:none" name= "Uploadfiles" id= "Uploadfiles" onload= "Iframeload ()" ></iframe>

ASP. NET MVC files are uploaded across domains, receiving returned results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.