Reputation: 15965
UPDATED QUESTION:
I'm trying to create a form which passes subject, content, and a file to a webservices. This is what I have so far and was wondering if someone could tell me if I am going in the right direction and how to do the bits which I have highlighted in the comments in the asmx file
The HTML:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form action="files.asmx/CaptureFile" enctype="multipart/form-data" method="post">
<input type="text" name="subject" /><br />
<input type="text" name="content" /><br />
<input type="file" name="filedata" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>
The WebService:
<%@ WebService Language="C#" Class="Files" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Services;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
[ScriptService]
public class Files : WebService {
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
int intAffectedRows;
[WebMethod()]
public int CaptureFile(string subject, string content, byte[] filedata)
{
// somehow reconstruct the filedata to an actual file saved on the server
// save subject, content, and filename to database
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand("query to save subject content and filename_only to database", connection))
{
command.Parameters.Add("@subject", SqlDbType.VarChar, 255).Value = subject;
command.Parameters.Add("@content", SqlDbType.VarChar, 255).Value = content;
command.Parameters.Add("@filedata", SqlDbType.VarChar, 255).Value = filedata; // need to save filename here, not file binary data
connection.Open();
intAffectedRows = command.ExecuteNonQuery();
connection.Close();
}
}
return intAffectedRows;
}
}
ORIGINAL QUESTION:
I understand how to send standard text to a webserver, process it, then send something back, i.e.
[WebMethod()]
public List<Notification> GetNotification(int id)
{
// do processing here
// return something back
return "Notification text";
}
My ajax looks like this:
$.ajax({
type: 'POST',
url: '/webservices/notifications.asmx/GetNotification',
data: '{id: ' + number + '}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
How do I send a file? The file will be a .pdf or a .doc file so I can save it to the server along with some text. So, I would want a textbox for subject, and select file button/textbox to select the file, and a submit button. When the 2 textboxes are filled in and the submit button is clicked, it should send the subject and file to the webservice, whwere the webservice will save the subject and file location to a database, and save the actual file to the server.
Also, I am developing on an intranet environment, and IE has full trust to the local intranet.
Upvotes: 4
Views: 7880
Reputation: 1039538
How do I send a file?
You can't send files using AJAX simply because using javascript you don't have access to the file system on the client computer so you cannot get the file contents. If you want to send files to a web server you could use an <form>
with file input and enctype="multipart/form-data"
which would post to a server side script. This script could then call the web service and transmit the file contents as an array of bytes for example.
Upvotes: 6