我正在使用 Quickbase C# SDK 从外部站点向 Quickbase 提交表单。我想随表格附上一个文件,但似乎不知道该怎么做。
下面是我的代码的精简版本:
ASPX
<form id="form1" runat="server">
<asp:TextBox ID="txtFileName" CssClass="textbox" Columns="40" runat="server"></asp:TextBox>
<input type="file" id="attachment1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
CS
protected void btnSubmit_Click(object sender, EventArgs e)
{
IQClient client = QuickBase.Login("username", "password", "domain");
IQApplication app = client.Connect("db_id", "app_token";
AppInfo appInfo = app.GetApplicationInfo();
IQTable table = app.GetTable("table_id");
IQRecord newRecord = table.NewRecord();
newRecord["File Name"] = txtFileName.Text;
// attach file?
newRecord.AcceptChanges();
table.AcceptChanges();
client.Logout();
}
提前致谢。
当然,在将问题提交到 StackOverflow 后我会弄清楚自己的问题。当然。
但是我会发布我的解决方案,以防其他人遇到同样的问题。
我必须向 QuickBase C# SDK 添加一个函数并重新编译 DLL 才能使其正常工作。
将此行添加到 IQRecord.cs:
void UploadFile(string columnName, string filePath);
将此函数添加到QRecord.cs中:
public void UploadFile(string columnName, string filePath)
{
// create new field with columnName
var index = GetColumnIndex(columnName);
CreateNewField(index, columnName);
// change field type to file
Columns[index].ColumnType = FieldType.file;
// Get field location with column index
var fieldIndex = _fields.IndexOf(new QField(Columns[index].ColumnId));
SetExistingField(index, fieldIndex, filePath);
}
像这样编译和使用:
// code to upload file to temporary location
newRecord.UploadFile("Story", "path_to_temporary_location");
// delete temporary file
public async Task<bool> UploadOrderTaxExemptQB()
{ bool isSuccess = false;
// Download file from the URL
using (HttpClient httpClient = new HttpClient())
{
HttpResponseMessage response = await httpClient.GetAsync("hn-to-your-Payoneer-account-Payoneer-Login.png");
if (response.IsSuccessStatusCode)
{
using (MemoryStream ms = new MemoryStream())
{
await response.Content.CopyToAsync(ms);
ms.Position = 0;
// Convert file to base64 string
byte[] fileBytes = ms.ToArray();
string base64File = Convert.ToBase64String(fileBytes);
// Prepare the body with the file attachment
var body = @"{
""to"":""fasdaer"",
""data"":[
{
""8"":{""value"":" + 2231 + @"},
""7"":{""value"":{""filename"":""TaxExemptDocument.png"",""file"":""" + base64File + @"""}},
""6"":{""value"":""Tax Exempt Document""}
}
],
""fieldsToReturn"":[3,6]
}";
var qbResponse = await helper.QuickBasePostInsertRequestAsync(body);
if (qbResponse.Content != null)
{
var typesResult = JsonConvert.DeserializeObject<QuickBaseDto>(qbResponse.Content);
foreach (var item in typesResult.data)
{
isSuccess = true;
}
}
}
}
}
return isSuccess;
}