尝试创建简单的文件传输Web服务

时间:2015-08-27 18:17:08

标签: c# asp.net .net asp.net-mvc

我正在尝试使用ASP.NET

创建一个非常简单的文件传输Web服务

我在线发现了这个教程:http://www.techrepublic.com/article/create-a-simple-file-transfer-web-service-with-net/

我做了以下事情:

  • 打开Visual Studio 2013
  • 创建了一个新的ASP.NET Web应用程序
  • 选定的模板Web表单
  • 添加了一个名为' FileRW.aspx'
  • 的新网络表单
  • 打开FileRW.aspx.cs并粘贴在以下代码中:

代码:

using System;
using System.Web.Services;
using System.Xml.Serialization;
using System.IO;

[WebService(Namespace = "http://someplace.com/FileIO/")]
public class FileRW : WebService
{

    [WebMethod]
    public byte[] GetFile(string filename)
    {
        BinaryReader binReader = new
 BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open,
 FileAccess.Read));
        binReader.BaseStream.Position = 0;
        byte[] binFile =
 binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
        binReader.Close();
        return binFile;
    }

    [WebMethod]
    public void PutFile(byte[] buffer, string filename)
    {
        BinaryWriter binWriter = new
 BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew,
 FileAccess.ReadWrite));
        binWriter.Write(buffer);
        binWriter.Close();
    }
}
  • 打开FileRW.aspx并粘贴以下内容:

代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileRW.aspx.cs" Inherits="WebApplication3.FileRW" %>

<script runat="server">

    // Insert page code here
    //

    void Button1_Click(object sender, EventArgs e) {
        string filename = "c:\\temp\\" + Guid.NewGuid().ToString() + ".tmp";
        file1.PostedFile.SaveAs(filename);
        System.IO.BinaryReader br = new
 System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open,
 System.IO.FileAccess.Read));
        FileIO.FileRW frw = new FileIO.FileRW();
        frw.Credentials = System.Net.CredentialCache.DefaultCredentials;
        br.BaseStream.Position = 0;
        Response.Write(br.BaseStream.Length);
        byte[] buffer = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));
        br.Close();
        frw.PutFile(buffer,
file1.PostedFile.FileName.Substring(file1.PostedFile.FileName.LastIndexOf("\\")
+ 1));
    }

</script>
<html>
<head>
</head>
<body>
    <form enctype="multipart/form-data" runat="server">
        <p>
            <input id="file1" type="file" name="file1" runat="server"/>
        </p>
        <p>
            &nbsp;<asp:Button id="Button1" onclick="Button1_Click"
 runat="server" Text="Button"></asp:Button>
        </p>
        <!— Insert content here —>
    </form>
</body>
</html>

将代码粘贴到FileRW.aspx后,我收到此错误&#39;类型或命名空间名称&#39; FileIO&#39;找不到(你是否错过了使用指令或汇编参考?&#39;

我的问题是我错过了什么装配?或者现在是否还有任何关于如何从服务器进行简单文件传输的教程,有点像FTP,列出文件并打开文件。

0 个答案:

没有答案