使用ASP.NET Web服务上传文件 [英] Upload file using asp.net webservice
问题描述
你好,我想从Web服务而不是WCF上传文件.我正在使用C#并将其与Web应用程序一起使用.从Web应用程序发送文件,服务将接受该文件(文本文件)并放置在网站/或上载文件夹中具体位置.
Hello I want to upload file from webservice not WCF.I am using C# and consuming it with web application.From web application send file,and service will accept that file(text file) and place in upload folder of website/or specific location.
为此,我创建了这样的网络服务
For this I have created webservice like this
:用于制作Web服务
创建了空的Web应用程序->选择了新项目-> Web服务
Created empty web application -> selected new Item -> Web service
1>在网络服务中编写以下代码
1> Wrote following code in webservice
public System.IO.Stream FileByteStream;
[WebMethod]
public void UploadFile()
{
FileStream targetStream = null;
Stream sourceStream = FileByteStream;
string uploadFolder = @"D:\UploadFile";
string filePath = Path.Combine(uploadFolder, @"C:\Users\maya\Desktop\test.txt");
using (targetStream = new FileStream(filePath, FileMode.Create,
FileAccess.Write, FileShare.None))
{
//read from the input stream in 65000 byte chunks
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
// save to output stream
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
sourceStream.Close();
}
在这里,我没有输入任何内容,我手动输入了一个文本文件.我想将该文件传输到uploadfolder.我收到此错误:
Here my not taking any input,I have manual entered one text file.I want to transfer that file to uploadfolder. I am getting this error:
HTTP 500内部服务器错误
HTTP 500 Internal server error
在此行:
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
如何处理?
推荐答案
我使用aspx页面而不是Web服务.我使用jquery来做到这一点.它像一种魅力.
I use an aspx page instead of a webservice.I use jquery to do so. It works like a charm.
所以让它成为您的文件上传控件.
so let this be your file upload control.
<div>
<input type="file" name="UploadFile" id="txtUploadFile" />
</div>
这是jquery代码
$('#txtUploadFile').on('change', function (e) {
var files = e.target.files;
if (files.length > 0) {
if (window.FormData !== undefined) {
var data = new FormData();
for (var x = 0; x < files.length; x++){
data.append("file" + x, files[x]);
}
$.ajax({
type: "POST",
url: '/Upload.aspx', //put the complete url here like http://www.example.com/Upload.aspx
contentType: false,
processData: false,
data: data,
success: function(result) {
console.log(result);
},
error: function (xhr, status, p3, p4){
var err = "Error " + " " + status + " " + p3 + " " + p4;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).Message;
console.log(err);
}
});
} else {
alert("This browser doesn't support HTML5 file uploads!");
}
}
});
这是我的aspx代码
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UploadFile(sender, e);
}
}
protected void UploadFile(object sender, EventArgs e)
{
try
{
HttpFileCollection fileCollection = Request.Files;
string savedfile = "";
for (int i = 0; i < fileCollection.Count; i++)
{
try
{
HttpPostedFile upload = fileCollection[i];
int f = fileCollection[i].ContentLength;
string filename = "/ProductImages/" + fileCollection[i].FileName;
upload.SaveAs(Server.MapPath(filename));
savedfile += fileCollection[i].FileName;
}
catch (Exception ex)
{
}
}
}
catch(Exception ex)
{
List<string> ff = new List<string>();
ff.Add(ex.Message.ToString());
System.IO.File.WriteAllLines(Server.MapPath("/ProductImages/Error.txt"), ff);
}
}
}
这篇关于使用ASP.NET Web服务上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!