The scenario is that if I have one internal domain server and one DMZ machine hosted in the cloud and for some reason we upload the file then we need to save the file on the internal server.
Now let us see how ot use WCF to save the file on the internal server.
Step 1
Create a WCF project in Visual Studio.
Now open the IServices class and add the following operation contract.
- [OperationContract]
- bool UploadToTempFolder(byte[] pFileBytes, string pFileName);
-
- [OperationContract]
- byte[] GetFileFromFolder(string pFileName);
Step 2Open the Services.svc class and the following code.
This method is used to save the file on the server.
- public bool UploadToTempFolder(byte[] pFileBytes, string pFileName)
- {
- bool isSuccess = false;
- FileStream fileStream = null;
-
- string strTempFolderPath = System.Configuration.ConfigurationManager.AppSettings.Get("FileUploadPath");
- try {
-
- if (!string.IsNullOrEmpty(strTempFolderPath))
- {
- if (!string.IsNullOrEmpty(pFileName))
- {
- string strFileFullPath = strTempFolderPath + pFileName;
- fileStream = new FileStream(strFileFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
-
- using(System.IO.FileStream fs = fileStream)
- {
- fs.Write(pFileBytes, 0, pFileBytes.Length);
- isSuccess = true;
- }
- }
- }
- } catch (Exception ex)
- {
-
- throw ex;
-
- }
-
- return isSuccess;
- }
-
- This method will give you the file content as a byte.
-
- public byte[] GetFileFromFolder(string filename)
- {
- byte[] filedetails = new byte[0];
- string strTempFolderPath = System.Configuration.ConfigurationManager.AppSettings.Get("FileUploadPath");
- if (File.Exists(strTempFolderPath + filename)) {
- return File.ReadAllBytes(strTempFolderPath + filename);
- } else return filedetails;
- }
Now our service is created and we will create the web application to consume the service and save the file.
Step 3Create an empty website from Visual Studio.
Add one file upload control and one button to upload the file.
- <table>
- <tr>
- <td>
- <span>Select File : </span>
- </td>
- <td>
- <asp:FileUpload ID="FileUpload1" runat="server" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button runat="server" ID="btnUpload" Text="Upload File" OnClick="btnUpload_Click" />
- </td>
- </tr>
- <tr>
- <td>
- <span>File Upload Starts on :</span>
- </td>
- <td>
- <asp:Label runat="server" ID="lblTimeStarts"></asp:Label>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button runat="server" ID="btnGetUploadedFile" OnClick="btnGetUploadedFile_Click" Text="Get Uploaded File" />
- <br />
- <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandName="GetFile" Text="
- <%# (string)Container.DataItem %>" />
- </ItemTemplate>
- </asp:DataList>
- </td>
- </tr>
- </table>
Add the service to the website. To do this right-click on the website and click on add web reference.
After adding the web reference add the following code to the button event.
- protected void btnUpload_Click(object sender, EventArgs e)
- {
-
- if(FileUpload1.HasFile)
- {
- System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
- timer.Start();
-
- #region Upload Files
-
- string filename = FileUpload1.FileName;
- byte[] filebyte = FileUpload1.FileBytes;
-
-
- FileUploadWCF.ServiceClient objAttachment = new FileUploadWCF.ServiceClient();
-
-
- objAttachment.UploadToTempFolder(filebyte, filename);
- #endregion
- timer.Stop();
- lblTimeStarts.Text = "File upload takes >>>" + Convert.ToString((TimeSpan.FromMilliseconds(timer.ElapsedMilliseconds).Milliseconds)) + " milliseconds. <br>";
-
- writetheFileNameInText(filename,"output");
- bindUploadFile(DataList1, "output");
-
- }
- }
- private void writetheFileNameInText(string fileName,string outputFileName)
- {
-
- using (StreamWriter w = File.AppendText(Server.MapPath(@"~\App_Data\" + outputFileName + ".txt")))
- {
- w.WriteLine(fileName);
- }
- }
- private void bindUploadFile(DataList dl,string outputFileName)
- {
- List<string> lstfileName = new List<string>();
- lstfileName = gettheFileNamefromText(outputFileName);
-
- dl.DataSource = lstfileName;
- dl.DataBind();
-
- }
Now execute the code and you will see the following screen.
After uploading the file it will show the files just below the Get Uploaded File.
Now this is in the development mode. If we host the service on IIS then we need to change the WCF binding depending on the requirements.
Please find the attached source code for WCF services and Temp web site.